「親カテゴリー一覧 > (親に属する)子カテゴリー一覧 > (子カテゴリに属する)投稿記事タイトル一覧」をするために、下記の記述にて実行しております。
現在、トップページ(front-page.php)にて親カテゴリーの表示、カテゴリーページ(category.php)にて子カテゴリーの表示はうまく行っています。
しかし、「投稿記事タイトル一覧」となると、(下記参照のcategory.phpでは)うまくいかず、カテゴリーIDを振ったcategory-ID.php(category-21.phpなど)で対応しています。
ただ、これだと、現在だけでも親が13個、子がそれぞれ4〜5個あるため、今後カテゴリー名が新規追加された場合、いちいち該当カテゴリーテンプレートを作らねばならずCMSとして現実的ではありません。
うまいやり方があればご教授いただきたくお願い致します。
【希望する表示】
たとえば、
〔親カテゴリー(トップページに表記)〕
・A
・B
・C
Aをクリックすると下記に遷移
〔親カテゴリーAに属する子カテゴリーページ〕
タイトルA
・A1
・A2
・A3
子カテゴリーをクリックすると、記事一覧に遷移
タイトルA1
・A1に属する記事タイトル1
・A1に属する記事タイトル2
・A1に属する記事タイトル3
そしてそれぞれクリックすると個別記事を表示する、といった具合です。
HOME >> タイトルA >> タイトルA1 >> A1に属する記事タイトル1
上記のようなパンくずリストを作成した場合、「タイトルA」のページでは、A1,A2,A3を表示してくれますが、タイトルA1では属する記事タイトルを列記したいのですが、何も表示してくれません(タイトルも親カテゴリーAのまま)。
以上は、category.phpのみを使用した場合ですが、強制的にcategoy-21.phpなどのID参照のテンプレートを使用すると、子カテゴリー選択時属する記事一覧が表示されるようになっていますが、カテゴリーの数分だけcategoy-1.php、categoy-2.php、…と内容は全く同じテンプレートを用意しなくてはいけないため、caegory-[ID],phpを自動的に生成してくれるやり方はないか、というご相談です。
(いずれは、親カテゴリーにアコーディオンメニューで表示予定)
※下記get_header()等の読込み記述は除きます。
【front-page.php記述】
<div id="contents"> <?php $categories = get_categories('parent=0'); foreach($categories as $category) : ?> <ul> <li><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->cat_name; ?></a></li> </ul> <?php endforeach; ?> </div><!-- #contents end -->
【functions.php記述】
//子カテゴリのアーカイブページを親カテゴリ用のテンプレートファイルで表示 add_filter( 'category_template', 'my_category_template' ); function my_category_template( $template ) { $category = get_queried_object(); if ( $category->parent != 0 && ( $template == "" || strpos( $template, "category.php" ) !== false ) ) { $templates = array(); while ( $category->parent ) { $category = get_category( $category->parent ); if ( !isset( $category->slug ) ) break; $templates[] = "category-{$category->slug}.php"; $templates[] = "category-{$category->term_id}.php"; } $templates[] = "category.php"; $template = locate_template( $templates ); } return $template; }
【category.php記述】
<div id="contents"> <h1 class="page_ttl"><?php single_cat_title (); ?></h1> <?php get_template_part('content-category'); ?> </div> <!-- #contents end -->
【(category.php読込み)content-category.php記述】
<div class="content2"> <?php $category_name = single_cat_title ('', false); // 現在のカテゴリ名を取得(表示はしないのでfalse) $category_id = get_cat_ID ($category_name); // カテゴリ名から現在のカテゴリIDを取得 $args = array( 'orderby' => 'count', // 投稿数順 'show_count' => 1, // 投稿数を表示 'child_of' => $category_id, // 現在のカテゴリの子カテゴリーを表示 'title_li' => '', // タイトル出さない 'show_option_none' => '', 'current_category' => 0, 'pad_counts' => 0, 'taxonomy' => 'category', // 投稿のカテゴリを指定 'walker' => null ); ?> <!-- カテゴリ一覧 --> <ul><?php wp_list_categories($args); ?></ul> </div><!-- .content2 end -->
ちなみに、
【(category-ID.phpの一部)category-51.php記述】
<div id="contents2"> <div class="section-header"> <?php $cat = get_the_category(); $cat = $cat[0]; $strcat = $cat->name; $catChildren = get_category_children($cat->cat_ID); //get_category_childrenは非推奨?get_term_childrenを使う? if ($catChildren) { $catIDs = explode('/',$catChildren); foreach($catIDs as $child) { $cat = get_category($child); if ($cat->slug == get_query_var('category_name')) { $strcat = $strcat.' - '.$cat->name; break; } } } ?> <h2><img src="<?php bloginfo('template_url'); ?>/images/ttl_triangle.gif" width="9" height="23" alt="" /> <?php echo $strcat; ?></h2> </div> <?php if(have_posts()): ?> <table class="cat_list"> <?php query_posts($query_string .'&orderby=post_tag&order=ASC');//query_postsは非推奨 ? ?> <?php while(have_posts()): the_post(); ?> <?php $cats = get_the_category(); $cats = $cats[0]; ?> <tr> <td class="cat_ttl"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td> </tr> <?php endwhile; ?> </table> <?php endif; ?> <!--sidebar --> <?php //get_sidebar (post); ?> <!--/sidebar END --> </div> <!--/container END -->
以上、長々とすみません。よろしくお願い致します。