カスタムタクソノミーで親子関係がある記事をすべて一覧表示したい
-
カスタム投稿のページテンプレート(archive-xxx.php)に
カスタムタクソノミーの親子関係がある記事をすべて一覧表示したいと思っています。下記のフォーラムを参考に出来そうだったのですが、
https://ja.forums.wordpress.org/topic/7786?replies=7
こちらはカテゴリーが必ず[親-子]という構造が前提で、
今回は子カテゴリーに分類されない記事もあるので、上記サイトのコードですと、
子カテゴリーに分類されない記事が表示されませんでした。下記のような構造にしたいのですが、何か良い方法はありますでしょうか?
■親Aカテゴリータイトル
・記事タイトル
・記事タイトル
・記事タイトル
■親Bカテゴリータイトル
○子Aカテゴリータイトル
・記事タイトル
・記事タイトル
・記事タイトル
○子Bカテゴリータイトル
・記事タイトル
・記事タイトル
・記事タイトルフォーラムを参考に作成したコードは以下です。
<?php $categories = get_terms('genre','orderby=order&order=ASC&parent=0'); foreach ( $categories as $cat ) { echo '<h2>' . esc_html($cat->name) . '</h2>'; $children = get_terms('genre','hierarchical=0&orderby=order&order=ASC&child_of='.$cat->term_id); foreach ( $children as $child ) { echo '<h3>' . esc_html($child->name) . '</h3>'; $catslug = $child->slug; $myquery = 'post_type=teacher&genre=' . $catslug . '&showposts=-1'; ?> <ul> <?php query_posts($myquery); ?> <?php if(have_posts()): while(have_posts()): the_post(); ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <?php endwhile; endif; ?> </ul> <?php } } ?>
- トピック「カスタムタクソノミーで親子関係がある記事をすべて一覧表示したい」には新たに返信することはできません。