サポート » 使い方全般 » カスタム分類アーカイブページで子タームの記事を除外したい。

  • 現在、下記の様な構造のカスタム投稿があります。

    ■カスタム投稿=country
    ■カスタム分類=country_list

    ■アメリカ(親ターム)
     ・学校1(記事)
     ・学校2(記事)
     ・学校3(記事)
     ■体験談(子ターム)
      ・体験談1(記事)
      ・体験談1(記事)
    ■中国(親ターム)
     ・学校1(記事)
     ・学校2(記事)
     ・学校3(記事)
     ■体験談(子ターム)
      ・体験談1(記事)
      ・体験談1(記事)

    このような作りで「アメリカ(親ターム)」のアーカイブページ(taxonomy.php)で記事の一覧を表示したいのですが、子タームの記事のみ非表示にしたいのです。

    以下の様に記述すると、ターム毎に記述を変えなければならないので、、それを避ける方法を教えていただけませんでしょうか。

    <?php
    $my_query = array(
    ‘post_type’ => ‘country’,
    ‘posts_per_page’ => -1,
    ‘tax_query’ => array( /* カスタム分類 */
    array(
    ‘taxonomy’ => ‘country_list’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘united-kingdom’,
    ‘include_children’ => false,
    )
    )
    );
    ?>

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • // 初期化
    $terms = $term_list = array();
    
    // country_listタクソノミーのトップ階層のタームのみ取得
    $terms = get_terms( 'country_list', array( 'parent' => '0' ) );
    
    // トップ階層のタームIDのリスト生成
    foreach( $terms as $term ):
    
    	$term_list[] = $term->term_id;
    
    endforeach;

    ループの内側で、記事ごとに判定:

    while ( have_posts() ) : the_post();
    
    	// トップ階層のタームに属している記事のみ表示
    	if( is_object_in_term( $post->ID, 'country_list', $term_list ) ):
    
    		(ループの内容)
    
    	endif;
    
    endwhile;

    こんな感じでしょうか?

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「カスタム分類アーカイブページで子タームの記事を除外したい。」には新たに返信することはできません。