エントリー一覧を含むカテゴリー一覧の出力
WordPress標準の「wp_list_categories」は、カテゴリを芋づる式に出力してくれるがエントリーまでは出してくれない。
そこで下記の「categories_and_entries」という関数を作った。
function.php に下記を追記。
長めなのでマーカー的にコメントを多用してます。
<?php function categories_and_entries( $catID ) { echo '<ul>'; /* UL1 */ $childs=get_categories( 'parent='.$catID.'&hide_empty=0'); foreach($childs as $child){ echo '<li><a href="'.get_category_link( $child->cat_ID ).'/">'. $child->cat_name .'</a>'; /* g_childs Loop */ $g_childs=get_categories( 'parent='.$child->cat_ID.'&hide_empty=0'); if($g_childs) { echo '<ul>'; /* UL2 */ foreach($g_childs as $g_child){ echo '<li><a href="'.get_category_link( $g_child->cat_ID ).'/">'. $g_child->cat_name .'</a>'; /* gg_childs Category Loop */ $gg_childs=get_categories( 'parent='.$g_child->cat_ID.'&hide_empty=0'); if($gg_childs) { echo '<ul>'; /* UL3 */ foreach($gg_childs as $gg_child){ echo '<li><a href="'.get_category_link( $gg_child->cat_ID ).'/">'. $gg_child->cat_name .'</a>'; /* gg_childs Entry Loop */ if (have_posts()) : query_posts('cat='.$gg_child->cat_ID.',&order=ASC'); echo '<ul>'; /* UL4 */ while (have_posts()) : the_post(); $tcat = get_the_category(); $tcat = $tcat[0]; if($gg_child->cat_ID == $tcat->cat_ID) { echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } endwhile; echo '</ul>'; /* END UL4 */ endif; /* END gg_childs Entry Loop */ } echo '</ul>'; /* END UL3 */ } /* END gg_childs Category Loop */ /* g_childs Entry Loop */ if (have_posts()) : query_posts('cat='.$g_child->cat_ID.',&order=ASC'); echo '<ul>'; /* UL5 */ while (have_posts()) : the_post(); $tcat = get_the_category(); $tcat = $tcat[0]; if($g_child->cat_ID == $tcat->cat_ID) { echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } endwhile; echo '</ul>'; /* END UL5 */ endif; /* END g_childs Entry Loop */ echo '</li>'; } /* childs Entry Loop */ if (have_posts()) : query_posts('cat='.$child->cat_ID.',&order=ASC'); //echo '<ul>'; /* UL6 */ while (have_posts()) : the_post(); $tcat = get_the_category(); $tcat = $tcat[0]; if($child->cat_ID == $tcat->cat_ID) { echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } endwhile; //echo '</ul>'; /* END UL6 */ endif; /* END childs Entry Loop */ echo '</ul>'; /* END UL2 */ } else {/* END g_childs Loop */ /* childs Entry Loop */ if (have_posts()) : query_posts('cat='.$child->cat_ID.',&order=ASC'); echo '<ul>'; /* UL7 */ while (have_posts()) : the_post(); $tcat = get_the_category(); $tcat = $tcat[0]; if($child->cat_ID == $tcat->cat_ID) { echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>'; } endwhile; echo '</ul>'; /* END UL7 */ endif; /* END childs Entry Loop */ } echo '</li>'; } echo '</ul>'; /* END UL1*/ wp_reset_query(); } ?>
呼び出し方は下記のように、関数に任意のカテゴリIDを記入する。
<?php categories_and_entries(4); ?>
指定したカテゴリの子・孫・ひ孫カテゴリまで、カテゴリ一覧とエントリー一覧を出力してくれる。
もしもっと深いカテゴリを出したい場合は、このコードが読める人ならカスタマイズは難しくないと思う。
かなりバタ臭いことしてるのはガッテン承知之助。
筆者について
- ゲームとジョジョを愛するファミッ子世代。好きな言葉は「機能美」。
公私ともにWebサービスを作る系男子。
最近の投稿
- 運用2017.03.03nginxのkusanagiにLet’sEncryptをかけてるとiOSで表示されない問題の解決
- 運用2016.07.22分かりにくい「クリエイティブコモンズ」をシンプルに言うと
- WordPress2016.07.12新しいWordPress「Calypso」を試した
- Design2016.03.09Win10アプリのデザインガイドラインが素晴らしい