Sponsored Link
カテゴリーページに、
親カテゴリーに属する子カテゴリーリストを表示して、
ページ内リンクを付与し、
その下に子カテゴリーの記事リストを表示する方法です。
Sponsored Link
カテゴリーページ(category.php)に有効です。
コラムなどの記事を書く時に役立つと思います。
後はQ&Aとかでも使えますね。
記事が存在していないカテゴリーは表示されないようになっています。
実際の例はこんな感じです。
↑だけだとわかりにくいので、
↓説明入れましたw
実際稼働しているサイトです。
get_termsは、指定したカテゴリーの子カテゴリーを配列で返します。
子カテゴリーを返す関数にget_category_childrenもありますが、
この関数を使うより、「記事がないカテゴリーは表示しない」というように、
より柔軟に制御することができます。
<?php $catentry = get_category(get_query_var('cat'),false); $catslug = $catentry->slug; //表示しているカテゴリーページのカテゴリースラッグ $catname = $catentry->name; //表示しているカテゴリーページのカテゴリー名 $catparentID = $catentry->parent; //表示しているカテゴリーページの親カテゴリーID $catchildID = $catentry->cat_ID; //表示しているカテゴリーページのカテゴリーID ?> <div class="entry clfx"> <h1><?php echo(get_category_parents($cat, false, '')); ?></h1> <?php $catchild = get_terms( 'category', array( 'child_of' => $catchildID,'hide_empty' => true, 'fields' => 'ids' ) ); ?> //表示しているカテゴリーページの子カテゴリーIDを配列で返します。記事が存在していないカテゴリーは表示しません。 <ul class="clumnlist clfx"> <?php foreach($catchild as $vall){ ?> <li><a href="<?php bloginfo('url'); ?>/<?php echo $catslug; ?>/#<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->slug; ?>" title="<?php echo $templatetag_cat->cat_name; ?>"><?php echo $templatetag_cat->cat_name; ?></a></li> <?php } ?> </ul> <?php foreach($catchild as $vall){ ?> <h2 id="<?php echo $templatetag_cat->slug; ?>"><?php echo $templatetag_cat->cat_name; ?></h2> <p><?php echo category_description($vall); ?></p> <ul class="postlist"> <?php $postclumn = get_posts('order=desc&category=' . $vall . '&posts_per_page=-1'); ?> <?php foreach($postclumn as $post): ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>
これで、記事が増えると自動的に追加されるのでラクです。
もしかしたら、もっとスマートな書き方があるかもしれない…。
けれど、メモとして残しておきます。
【追記】
カテゴリーリストのURLをもっとスマートな書き方にしました。
<?php $catentry = get_category(get_query_var('cat'),false); $catslug = $catentry->slug; //表示しているカテゴリーページのカテゴリースラッグ $catname = $catentry->name; //表示しているカテゴリーページのカテゴリー名 $catparentID = $catentry->parent; //表示しているカテゴリーページの親カテゴリーID $catchildID = $catentry->cat_ID; //表示しているカテゴリーページのカテゴリーID ?> <div class="entry clfx"> <h1><?php echo(get_category_parents($cat, false, '')); ?></h1> <?php $catchild = get_terms( 'category', array( 'child_of' => $catchildID,'hide_empty' => true, 'fields' => 'ids' ) ); $category_link = get_category_link($catchildID); ?> <ul class="clumnlist clfx"> <?php foreach($catchild as $vall){ ?> <li><a href="<?php echo esc_url( $category_link ); ?>#<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->slug; ?>" title="<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->cat_name; ?>"><?php $templatetag_cat = get_category($vall);echo $templatetag_cat->cat_name; ?></a></li> <?php } ?> </ul> <?php foreach($catchild as $vall){ ?> <h2 id="<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->slug; ?>"><?php echo $templatetag_cat->cat_name; ?></h2> <p><?php echo category_description($vall); ?></p> <ul class="postlist"> <?php $postclumn = get_posts('order=desc&category=' . $vall . '&numberposts=-1'); ?> <?php foreach($postclumn as $post): ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>
例えば、特定のカテゴリーページに表示したい場合(カテゴリーIDが27の例)、
<?php if(is_category('27') || in_category('27') ){ ?> //ここに上記のソースを入れる。 <?php } ?>
もし、シングルページにもカテゴリーリストを表示したい場合は、
single.phpに下記を挿入します。
27の部分を、表示したいカテゴリーIDに変えてくださいね。
<?php $parentcat = get_the_category(); $parentID = $parentcat[0]->parent; $carrentslug = $parentcat[0]->category_nicename; $carrentname = $parentcat[0]->cat_name; $carrentID = $parentcat[0]->cat_ID; $catchild = get_terms( 'category', array( 'child_of' => 27,'hide_empty' => true, 'fields' => 'ids' ) ); $category_link = get_category_link($parentID); ?> <?php if($parentID == 27){ ?> <div class="entry clfx"> <h1><?php echo $carrentname; ?></h1> <ul class="clumnlist clfx"> <?php foreach($catchild as $vall){ ?> <li><a href="<?php echo esc_url( $category_link ); ?>#<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->slug; ?>" title="<?php $templatetag_cat = get_category($vall);echo $templatetag_cat->cat_name; ?>"><?php $templatetag_cat = get_category($vall);echo $templatetag_cat->cat_name; ?></a></li> <?php } ?> </ul> <?php } ?>