記事が属する複数のタームに属している記事一覧
-
記事が属する複数のタームに属している記事の一覧を記事ページの下部に表示したいと思い、以下の記述で設置したところ、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件中)
4件の返信を表示中 - 1 - 4件目 (全4件中)
- トピック「記事が属する複数のタームに属している記事一覧」には新たに返信することはできません。