[WordPress]タームごとの記事一覧を表示する方法

WordPress
スポンサーリンク

タームごとの記事一覧を表示

<?php
$taxonomy_name = 'タクソノミースラッグ';
$taxonomys = get_terms($taxonomy_name);
if(!is_wp_error($taxonomys) && count($taxonomys)):
  foreach($taxonomys as $taxonomy):
  $args = array(
    'post_type' => get_post_type(),
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomy_name,
        'field' => 'slug',
        'terms' => $taxonomy->slug,
      ),
    ),
  );
  $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()): ?>
  <?php echo esc_html($taxonomy->name); ?>
  <ul>
  <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
  <li><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></li>
  <?php endwhile; ?>
  </ul>
<?php endif;?>
<?php wp_reset_postdata();?>
<?php endforeach;?>
<?php endif;?>

コメント

タイトルとURLをコピーしました