get_posts を使って記事(投稿)を取得していると思いますが、違っていたら読み飛ばしてください。
階層を持つタクソノミーの場合、親タームについて投稿を取得すると、その子孫タームが付いた投稿も一緒に取得されるのがデフォルトの動作です。これは普通のカテゴリー(カスタムタクソノミーではない)について投稿を取得しても同様の結果になります。
そのデフォルト動作ではなく、「子孫タームは含めず、指定したタームに限る」という検索条件に変えるには、tax_query の include_children 引数を false にします(デフォルトは true です)。
WP_Query の説明ページに include_children の説明があります。なお、日本語Codexの WP_Query のページは少し古いので本家英語版を見てください。
デフォルト(include_children が true):
$myposts = get_posts( 'post_type=ABC&mytaxonomy=XYZ' );
include_children を false に変える:
$args = array(
'post_type' => 'ABC',
'tax_query' => array(
array(
'taxonomy' => 'mytaxonomy',
'field' => 'slug',
'terms' => 'XYZ',
'include_children' => false,
)
)
);
$myposts = get_posts( $args );
gblsmaさん ご返信ありがとうございます!
無知でもうしわけありません。
いただいたコードを参考にしたのですが、termsでtermを設定しなければならないので複数設定しなければいけない感じなのでしょうか?
私が書いたコード↓
`
<?php
$args = array(
‘post_type’ => ‘category_01’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘category01_classify’,
‘field’ => ‘slug’,
‘terms’ => ‘category01_02_01’,
‘include_children’ => false
)
)
);
$my_posts = get_posts( $args );
if ( $my_posts ) {
foreach ( $my_posts as $post ) :
setup_postdata( $post );
?>
<li>
<dl>
<dt><?php the_title(); ?></dt>
<dd><a href=”<?php the_permalink(); ?>”>リンク</a></dd>
</dl>
</li>
<?php
endforeach;
} else {
echo ‘このカテゴリに投稿はありません’;
}
wp_reset_postdata();
?>
------
【表示目標】
category01_01
- 記事01
category01_02
- category01_02_01
-- 記事02
-- 記事03
- category01_02_02
はこのように、記事が子タームに属している場合には、親ターム直下には表示されずに入れ子関係が維持されているような形です。
【記事が子タームに属していたら○○】【それ以外は○○(親ターム直下に表示する】のような書き方ができるといいなと思っているのですが、難しいのでしょうか。。。。
----
ちなみに、一番はじめに参考URLを参考にしていたときの書き方は↓になります。
これだと、一番はじめに投稿させていただいたような2重に表示されてしまいました。
<?php
$catargs = array(
‘taxonomy’ => ‘category01_classify’
);
$catlists = get_categories( $catargs );
foreach($catlists as $cat) :
?>
<li class=”head”><?php echo $cat->name; ?></li>
<?php
$args = array(
‘post_type’ => ‘category_01’,
‘category01_classify’ => $cat->slug
);
$my_posts = get_posts( $args );
if ( $my_posts ) { // 該当する投稿があったら
foreach ( $my_posts as $post ) :
setup_postdata( $post );
?>
<li class=”lv1″>
<dl>
<dt><?php the_title(); ?></dt>
<dd><a href=”<?php the_permalink(); ?>”>リンク</a></dd>
</dl>
</li>
<?php
endforeach;
} else {
echo ‘このカテゴリに投稿はありません’;
}
wp_reset_postdata();
?>
<?php endforeach; ?>
`
—-
お知恵ばかり拝借するようで恐縮ですが、よろしくお願いします。
連投すいません。。。
<?php
$categories = get_terms('category01_classify','orderby=order&order=ASC&parent=0');
foreach ( $categories as $cat ) {
echo '<h1>' . esc_html($cat->name) . '</h1>';
$children = get_terms('category01_classify','hierarchical=0&orderby=order&order=ASC&child_of='.$cat->term_id);
foreach ( $children as $child ) {
echo '<h2>' . esc_html($child->name) . '</h2>';
$catslug = $child->slug;
$args = 'post_type=category_01&category01_classify=' . $catslug . '&showposts=-1';
query_posts( $args );
if ( have_posts() ) :
$output .= '<ul>';
while ( have_posts() ) : the_post();
$output .= '<li><a href=" '. get_permalink() .' ">';
$output .= get_the_title() .'</a></li>';
endwhile;
$output .= '</ul>';
echo $output;
wp_reset_query();
endif;
}
}
?>
参考URL
https://ja.forums.wordpress.org/topic/7786?replies=7
↑を参考に、汚いですが書き換えてみました。
【表示】
category01_01
category01_02
- category01_02_01
– 記事02
– 記事03
- category01_02_02
のように表示され、親タームに属している記事だけ表示されません。
(phpでechoしていないので当たり前なのですが悩んでいます。。)
なにかアドバイスいただけないでしょうか。。
これでどうでしょう。
<?php
function my_terms_posts( $cat, $level = 0 ) {
// トップレベルだけ h1 タグ、他は h2 タグ。
if ( 0 == $level ) {
echo '<h1>' . $level . ':' . esc_html( $cat->name ) . '</h1>';
} else {
echo '<h2>' . $level . ':' . esc_html( $cat->name ) . '</h2>';
}
// 指定されたタームに属する投稿を表示。子孫タームは除く。
$args = array(
'post_type' => 'category_01',
'tax_query' => array(
array(
'taxonomy' => 'category01_classify',
'terms' => $cat->term_id,
'include_children' => false
)
)
);
$myposts = get_posts( $args );
if ( ! empty( $myposts ) ) {
$output = '<ul>';
foreach ( $myposts as $po ) {
$output .= '<li><a href="' . get_permalink( $po->ID ) . '">';
$output .= get_the_title( $po->ID ) .'</a></li>';
}
echo $output . '</ul>';
}
// 直接の子タームだけについて、この関数を再帰呼び出し。
$children = get_terms( 'category01_classify', 'parent=' . $cat->term_id );
if ( ! empty( $children ) ) {
echo '<ul>';
foreach ( $children as $child ) {
echo '<li>';
my_terms_posts( $child, $level + 1 );
echo '</li>';
}
echo '</ul>';
}
}
// トップレベルのタームだけ、上の関数を実行。
$tops = get_terms( 'category01_classify', 'parent=0' );
foreach ( $tops as $cat ) {
my_terms_posts( $cat );
}
?>
gblsmさん
またまたアドバイスいただきありがとうございます!
いただいたコードにて実装してみたところ思った通りの挙動になっております!!
勉強不足の中で大変恐縮ですが、本当にありがとうございました。
助かりました!
こちらにてこの質問は解決済みとさせていただきます。