###前提・実現したいこと
お世話になっております。
表題の件について、現在のカテゴリ情報の一覧と投稿一覧を表示させたく質問しました。
参考サイトのコードですと、現在のページからではなく、全カテゴリの一覧と投稿記事が表示されてしまい、4階層目・5階層目のカテゴリが表示できないため少しコードを加えてみましたが上手く出来ません。。(コードも汚いです)
カテゴリだけ表示させるならwp_list_categoriesを使えば良かったのですが、最下層のカテゴリを表示させたいのでその記述方法についても教えて頂ければ幸いです。
WordPress タームの親・子・孫の一覧に孫タームの投稿一覧を表示する方法
ワードプレスで3階層までカテゴリを固定ページテンプレートで表示
###表示させたいこと
<section> <h1><span>親カテゴリタイトル</span></h1> <h2>子カテゴリA_タイトル(2階層目)</h2> <h2>子カテゴリB_タイトル(2階層目)</h2> <h3 class="cat-item category-child-three">孫カテゴリA_タイトル(3階層目)</h3> <h3 class="cat-item category-child-three">孫カテゴリB_タイトル(3階層目)</h3> <h4 class="category-child-four">4階層目カテゴリA_タイトル</h4> <h4 class="category-child-four">4階層目カテゴリB_タイトル</h4> <h5 class="category-child-five">5階層目カテゴリA_タイトル</h5> <!-- 5階層目カテゴリに紐づく投稿一覧 --> <div class="bottom_layer"> <ul class="post_list"> <li><a href="#">投稿タイトル</a></li> <li><a href="#">投稿タイトル</a></li> <li><a href="#">投稿タイトル</a></li> <li><a href="#">投稿タイトル</a></li> </ul> </div> </section>
###試したこと(まだカテゴリ表示のみ)
<section> <?php $children = get_the_category($post->ID,'category'); $current_cats = get_queried_object(); ?> <?php if($children) : ?> <?php $cats_id = get_category_by_slug($category_name)->term_id; $args = array('orderby' => 'name', 'order' => 'ASC','child_of' => $current_cats->term_id ); $categories = get_categories($args); ?> <h1><span><?php single_cat_title(); ?></span></h1> <?php foreach($categories as $category){ echo '<h2>' . $category->name . '</h2>'; // 第3階層 $category_ids = get_term_children($category->cat_ID,'category'); $category_counts = count($category_ids); $children_args = array('parent'=>$category->cat_ID); $child_category_objects = get_categories($children_args); $child_category_objects_pointer = $child_category_objects; if($category_counts > 0){ foreach($child_category_objects as $child_category_object){ $child_category_id = $child_category_object -> cat_ID; echo '<h3 class="cat-item category-child-three">' . $child_category_object -> name .'</h3>'; // 第4階層 $grandson_categories_ids = $child_category_object -> cat_ID; $grandson_categories_name = $child_category_object -> name; $grandson_categories_count = count(get_term_children($grandson_categories_ids,'category')); if($grandson_categories_count > 0){ $grandson_category_args = array('parent'=>$grandson_categories_ids); $grandson_category_objects = get_categories($grandson_category_args); $grandson_category_objects_pointer = $grandson_category_objects; foreach($grandson_category_objects as $grandson_category_object){ echo '<h4 class="category-child-four">' . $grandson_category_object -> name . '</h4>'; // 第5階層 $greatgrandson_categories_ids = $grandson_category_object -> cat_ID; $greatgrandson_categories_name = $grandson_category_object -> name; $greatgrandson_categories_count = count(get_term_children($greatgrandson_categories_ids,'category')); if($greatgrandson_categories_count > 0){ $greatgrandson_category_args = array('parent'=>$greatgrandson_categories_ids); $greatgrandson_category_objects = get_categories($greatgrandson_category_args); $greatgrandson_category_objects_pointer = $greatgrandson_category_objects; foreach($greatgrandson_category_objects as $greatgrandson_category_object){ echo '<h5 class="category-child-five">' . $greatgrandson_category_object -> name . '</h5>'; } } } } } } } ?> <?php endif; ?> </section>
出力結果(◎があるとこは重複してます)
<section> <h1><span>親カテゴリタイトル</span></h1> <h2>子カテゴリA_タイトル(2階層目)</h2> <h2>子カテゴリB_タイトル(2階層目)</h2> <h3 class="cat-item category-child-three">孫カテゴリA_タイトル(3階層目)</h3> <h3 class="cat-item category-child-three">孫カテゴリB_タイトル(3階層目)</h3> <h4 class="category-child-four">4階層目カテゴリA_タイトル</h4> <h4 class="category-child-four">4階層目カテゴリB_タイトル</h4> <h5 class="category-child-five">5階層目カテゴリA_タイトル</h5> <h2>孫カテゴリA_タイトル(3階層目)</h2>◎ <h2>孫カテゴリB_タイトル(3階層目)</h2>◎ <h3 class="cat-item category-child-three">4階層目カテゴリA_タイトル</h3>◎ <h3 class="cat-item category-child-three">4階層目カテゴリB_タイトル</h3>◎ <h4 class="category-child-four">5階層目カテゴリA_タイトル</h4>◎ <h2>4階層目カテゴリA_タイトル</h2>◎ <h2>4階層目カテゴリB_タイトル</h2>◎ <h3 class="cat-item category-child-three">5階層目カテゴリA_タイトル</h3>◎ </section>
カテゴリ
親カテゴリ ├子カテゴリA └子カテゴリB ├孫カテゴリA └孫カテゴリB ├4階層目カテゴリA └4階層目カテゴリB └5階層目カテゴリA
###追記
テンプレートはcategory.phpになります。
今現状のコードはまだカテゴリを表示させている段階で、投稿一覧を表示させるのには参考サイトのコードでもいいかなと考えていますが、コードが汚いのでもう少し簡潔に記述できる方法がありますでしょうか…?
お力添えをいただければ幸いです。
回答1件
あなたの回答
tips
プレビュー