みなさんはじめまして!
主にマークアップの担当をしている和田です。
何かハンドルネームを考えようかと思ったのですが、よくよく考えたらすでに投稿者名が表示されていることに気付きました。
これから度々ブログを書くと思いますが、よろしくお願いします!
さて、初回の今回は、コーディング工房流 WordPressチートシートです。
プラグインの紹介などなども入れようと思ったのですが、それを入れるとネタの消費が激しくなるので、後日別途投稿したいと思います。
※<body>タグ直前に<?php wp_head(); ?>、</body>タグ直前に<?php wp_footer(); ?>を忘れないようにする。前述2つを忘れていると、動作しないプラグインがある。(例:SyntaxHighlighter Evolvedなど)
※アイキャッチ画像の使用するには、function.phpでオプション関数の指定必須。他にも、function.phpをカスタマイズすることによって、様々な機能を実装することが可能です。
また、styleシートには、以下の記述が必須です。
/* Theme Name: テーマの名前 Theme URI: テーマのホームサイトのURI Description: テーマの説明 Author: 作成者 Version: バージョン Tags: タグ */
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
詳細は、「自由にデザインできるカウンタ付ツイートボタンを作ってみた」を参照。
function.php:
function get_twitter_count($url) {
$json = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=".urlencode($url));
$data = json_decode($json, true);
return $data[count];
}
テンプレート:
<?php $tweet_url = "http://twitter.com/share?url=".get_permalink()."&text=".urlencode(get_the_title()."|".get_option('blogname')); ?>
<a href="javascript:void(0);" onclick="window.open(
'<?php echo $tweet_url ?>',
'twitter_tweet',
'left='+Math.round((screen.width/2)-225)+',
top=100,
width=550,
height=450,
personalbar=0,
toolbar=0,
scrollbars=1,
resizable=1'
)"><?php echo get_twitter_count(get_permalink()) ?> tweets</a>
function get_breadcrumbs(){
global $wp_query,$post;
if ( !is_home() ){
// Start the UL
echo "\t\t\t\t\t<ul>\n";
// Add the Home link
echo "\t\t\t\t\t\t<li><a href=\"". get_settings('home') .'">'. get_bloginfo('name') ."</a></li>\n";
if ( is_category() )
{
$catTitle = single_cat_title( "", false );
$cat = get_cat_ID( $catTitle );
$category_list = split("/", get_category_parents( $cat, false, "/" ));
foreach( $category_list as $catsTitle ) {
if($catsTitle != "") {
$cats = get_category(get_cat_ID($catsTitle));
$cats_publ = $cats->cat_ID == $cat ? $cats->cat_name : '<a href="'.get_category_link($cats).'">'.$cats->cat_name.'</a>';
echo "\t\t\t\t\t\t<li>".$cats_publ."</li>\n";
}
}
}
elseif ( is_archive() && !is_category() )
{
echo "\t\t\t\t\t\t<li>Archives</li>\n";
}
elseif ( is_search() ) {
echo "\t\t\t\t\t\t<li>Search Results</li>\n";
}
elseif ( is_404() )
{
echo "\t\t\t\t\t\t<li>404 Not Found</li>\n";
}
elseif ( is_single() )
{
$category = get_the_category();
$cat = $category[0]->cat_ID;
$category_list = split("/", get_category_parents( $cat, false, "/" ));
foreach( $category_list as $catsTitle ) {
if($catsTitle != "") {
$cats = get_category(get_cat_ID($catsTitle));
$cats_publ = '<a href="'.get_category_link($cats).'">'.$cats->cat_name.'</a>';
echo "\t\t\t\t\t\t<li>".$cats_publ."</li>\n";
}
}
$queID = in_category_parent(CAT_ID_BLOG) ? url_to_postid(get_bloginfo('siteurl')."/".$wp_query->query_vars[category_name]."/".$wp_query->query_vars[name]) : '';
echo "\t\t\t\t\t\t<li>".get_the_title($queID)."</li>\n";
}
elseif ( is_page() )
{
$post = $wp_query->get_queried_object();
if ( $post->post_parent == 0 ){
echo "\t\t\t\t\t\t<li>".the_title('','', FALSE)."</li>\n";
} else {
$title = the_title('','', FALSE);
$ancestors = array_reverse( get_post_ancestors( $post->ID ) );
array_push($ancestors, $post->ID);
foreach ( $ancestors as $ancestor ){
if( $ancestor != end($ancestors) ){
echo "\t\t\t\t\t\t<li>".'<a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) ."</a></li>\n";
} else {
echo "\t\t\t\t\t\t<li>". strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) ."</li>\n";
}
}
}
}
// End the UL
echo "\t\t\t\t\t</ul>\n";
}
}
テンプレート:
<?php get_breadcrumbs(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
コンテンツを表示
<?php endwhile; ?>
<?php else : ?>
コンテンツが無い時の表示
<?php endif; ?>
<?php
$my_query = new WP_Query('showposts=表示件数&cat=カテゴリーID');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
コンテンツ
<?php endwhile; ?>