サポート » 使い方全般 » 記事が属する複数のタームに属している記事一覧

  • 解決済 nobugot

    (@nobugot)


    記事が属する複数のタームに属している記事の一覧を記事ページの下部に表示したいと思い、以下の記述で設置したところ、1件のみの表示になってしまいます。

    <?php $term = array_shift(get_the_terms($post->ID, 'タクソノミー名')); ?>
    <?php $args = array(
    'post_type' => 'post',
    'tax_query' => array(
    array(
    'taxonomy' => 'タクソノミー名',
    'field' => 'slug',
    'terms' => $term->slug
    ));
    $the_query = new WP_Query( $args ); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    ここに内容を表示
    
    <?php endwhile; else: ?>
    <?php endif; ?>

    調べてみると array_shift で配列の1つめだけを取得しているため?と思われるのですが、以下のように array_shift をとると何も表示されなくなってしまいます。

    <?php $term = get_the_terms($post->ID, 'タクソノミー名'); ?>
    <?php $args = array(
    'post_type' => 'post',
    'tax_query' => array(
    array(
    'taxonomy' => 'タクソノミー名',
    'field' => 'slug',
    'terms' => $term->slug
    ));
    $the_query = new WP_Query( $args ); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    ここに内容を表示
    
    <?php endwhile; else: ?>
    <?php endif; ?>

    $term->slug のあたりが怪しいとおもうのですが、どのように記述するとよいのでしょうか?

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • 例えば記事にタームが2つ付いている(スラッグがtermslug1、termslug2)としますね。この2つが付いている記事を全部取得するには次のようなコードを使います。

    $args = array(
      'tax_query' => array(
        'relation' => 'AND',
        array(
          'taxonomy' => 'タクソノミー名',
          'field' => 'slug',
          'terms' => 'termslug1'
        ),
        array(
          'taxonomy' => 'タクソノミー名',
          'field' => 'slug',
          'terms' => 'termslug2'
        )
      )
    );
    $the_query = new WP_Query( $args );

    トピック投稿者 nobugot

    (@nobugot)

    ありがとうございます。
    ‘terms’ => array(‘AAA’,’BBB’)
    の形にするため、試行錯誤して、以下のコードでなんとか希望どうりの表示ができました。
    なにかもっと他にシンプルな方法はあるのでしょうか?

    <?php $terms = get_the_terms( $post->ID, 'タクソノミー名' );
    if ( $terms && ! is_wp_error( $terms ) ) {
    $value = array();
    foreach ( $terms as $term ) {
    $value[] =  $term->slug;
    }
    }
    ?>
    
    <?php $args = array(
    'post_type' => 'post',
    'tax_query' => array(
    array(
    'taxonomy' => 'タクソノミー名',
    'field' => 'slug',
    'terms' => array($value)
    ));
    $the_query = new WP_Query( $args ); ?>

    もっとシンプルにということでは、PHPのバージョンが7.0以降なら array_column() が使えるかもしれません(foreachループの代わりに)。

    それから terms パラメータの値は、array($value) だと配列の配列になってしまうので、$value でよいと思います。

    トピック投稿者 nobugot

    (@nobugot)

    記述をミスしておりました。以下修正版です。
    ありがとうございました!

    <?php $terms = get_the_terms( $post->ID, 'タクソノミー名' );
    if ( $terms && ! is_wp_error( $terms ) ) {
    $value = array();
    foreach ( $terms as $term ) {
    $value[] =  $term->slug;
    }
    }
    ?>
    
    <?php $args = array(
    'post_type' => 'post',
    'tax_query' => array(
    array(
    'taxonomy' => 'タクソノミー名',
    'field' => 'slug',
    'terms' => $value
    ));
    $the_query = new WP_Query( $args ); ?>
4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「記事が属する複数のタームに属している記事一覧」には新たに返信することはできません。