例えばトップページとカテゴリーページでサイドバーの表示を変えたい場合など、コンディショナルタグ(条件分岐)で振り分けることが出来る。
トップページのみ「AAA」を表示する、なら
<?php if (is_home()): ?>AAA<?php endif; ?>
トップページ以外「AAA」を表示する、なら
<?php if (!is_home()): ?>AAA<?php endif; ?>
と書く。
トップページのみ「AAA」を表示、それ以外は「BBB」を表示、なら
<?php if (is_home()): ?>AAA<?php else: ?>BBB<?php endif; ?>
トップページは「AAA」、カテゴリー3で「BBB」を表示、なら
<?php if (is_home()): ?>AAA<?php elseif (is_category('3')): ?>BBB<?php endif; ?>
トップページは「AAA」カテゴリー3と7で「BBB」を表示、なら
<?php if (is_home()): ?>AAA<?php elseif (is_category(array(3,7)): ?>BBB<?php endif; ?>
シングルページで、かつ、カテゴリー3に属している場合「BBB」を表示、なら
<?php if (is_single() && in_category(3): ?>BBB<?php endif; ?>
条件付け部分の書き方についてはhttp://wpdocs.sourceforge.jp/Conditional_Tagsを参照。
*カテゴリーページでカテゴリーを条件付けする場合は is_category 、シングルページでカテゴリーを条件付けする場合は in_category と書く。IDでなくスラッグで指定しておくとサイトを引っ越しした際なども書き換えなくて良い。is_single(array(‘xxx’, ‘aaa, ‘bbb’)) のように書くことも出来る。
親カテゴリーの内容・記述を子カテゴリーにも適用したい時
<?php if (cat_is_ancestor_of(36, $cat) or is_category(36)): ?>
とする。single ページでの分岐は
functions.php: function in_category_family( $parent ) { if ( empty($parent) ) return false; if ( in_category($parent) ) return true; $parent = get_category($parent); foreach ( (get_the_category()) as $child ) { $child = get_category($child->cat_ID); if ( cat_is_ancestor_of($parent, $child) ) return true; } return false; } 分岐: if ( function_exists('in_category_family') && in_category_family(1) )
個別ページのナビゲーション(同一カテゴリー内)で前後両方の記事がある場合のみ、間に仕切り線を入れる。*アーカイブの場合はget_next_posts_link()
<?php next_post_link('%link', '%title', TRUE); ?><?php if(get_next_post('true','') && get_previous_post('true','')): ?> | <?php endif; ?><?php previous_post_link('%link', '%title', TRUE); ?>
固定ページで子ページの分岐 *数字はID
<?php if ( '2' == $post->post_parent || '9' == $post->post_parent ): ?>
コメントがオープンかどうか
<?php if (comments_open()): ?>
パスワードが付いているかどうか
<?php if (!post_password_required()): ?> ~ <?php else : ?> <?php echo get_the_password_form(); ?> <?php endif; ?>
2012.5追記
子カテゴリー内の投稿かどうか
functions.php: function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category'); if ( $descendants && in_category( $descendants, $_post ) ) return true; } return false; } 分岐: if ( post_is_in_descendant_category( 1 ) )
2012.7追記
シングルページ内で親カテゴリーでの振り分け
<?php $cat = get_the_category(); $cat = $cat[0]; $cat_id = $cat->cat_ID; $catp = $cat->category_parent; ?><?php if ($catp == 1 || $cat_id == 1): ?>カテゴリー1の場合の内容<?php elseif ($catp == 3 || $cat_id == 3): ?>カテゴリー3の場合の内容<?php endif; ?>
2013.1追記
親ページがある時、ページタイトルと親ページのタイトルを表示
<?php global $post; if ( $post->post_parent ) { $parent = get_the_title($post->post_parent); echo $post->post_title . ' | ' . $parent; } else { echo $post->post_title; } ?>
タクソノミー用テーマで親タームがある時に親ターム名を表示 *タクソノミー名item の場合
<?php $term_slug = get_query_var('item'); $term_info = get_term_by('slug',$term_slug,'item'); echo $term_info->name; ?> | <?php if ($term_info->parent) { $parent = get_term( $term_info->parent,'item' );echo $parent->name . ' | ';} ?>
2013.9.1追記
最下層のカテゴリーから親カテゴリーを取得し、そのIDで分岐 ↓親カテゴリーのIDが7の場合7と表示
<?php $cats = get_the_category(); $current_cat = ''; foreach ( $cats as $cat ) { if ( ! $current_cat || cat_is_ancestor_of( $current_cat, $cat ) ) { $current_cat = $cat; } } if ( in_array(7, get_ancestors($current_cat->cat_ID, 'category') )) { echo '7'; } ?>