- wordpress
ターム別の記事一覧
最終更新日:2020/03/12
前回の記事で、タクソノミーの設定についてまとめました。
今回は、その作成したタクソノミーのターム別記事一覧を作成します。
</>wordpress
<?php// タクソノミー(カテゴリ)別に記事一覧
$terms = get_terms( 'area' );
foreach ( $terms as $term ) :
$args = array(
'post_type' => 'post',
'taxonomy' => 'area',
'term' => $term->slug,
'posts_per_page' => -1,
'no_found_rows' => true,
);
$query = new WP_Query($args); ?>
<ul>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
<?php endforeach; ?>
2行目と6行目に設定したタクソノミー名を入れます。この場合はareaにしました。
5行目は、投稿タイプの名前を入れます。今回は通常の投稿なので、postにしました。