WordPressで指定したタクソノミーの親ターム・子タームのリストとそれぞれのタームに属する記事の一覧を出力する方法

WordPressで指定したタクソノミーの親ターム・子タームのリストとそれぞれのタームに属する記事の一覧を出力する方法

WordPressで指定したタクソノミーの親ターム・子タームのリストとそれぞれのタームに属する記事の一覧を出力する方法

指定したタクソノミーの親タームと子タームのリストを表示しつつ、子タームに属する記事の一覧も一緒に出力する方法をご紹介いたします。

ちょっとイメージし難いかもしれませんが、以下のようにタームのリストと記事一覧を合わせて表示させます。

  • 親ターム名1
  • 子ターム名1
    • 子ターム1に属する記事1
    • 子ターム1に属する記事2
    • 子ターム1に属する記事3
  • 親ターム名2
  • 子ターム名2
    • 子ターム2に属する記事1
    • 子ターム2に属する記事2
    • 子ターム2に属する記事3
スポンサードリンク

指定したタクソノミーの親ターム・子タームのリストとそれぞれのタームに属する記事の一覧を出力する方法

親ターム・子タームのリストとそれぞれのタームに属する記事の一覧を表示させたい場合、テーマファイル内で以下のように記述します。


<?php
$taxonomyName = "タクソノミー名";
$args = array(
  'parent' => 0
);
$terms = get_terms($taxonomyName,$args);
foreach ($terms as $term) { ?>
  <h2 id="<?php echo $term->slug; ?>"><?php echo $term->name; ?></h2>
  <?php $children = get_terms($taxonomyName,'hierarchical=0&parent='.$term->term_id); if(!$children): //子タームが設定されていない場合 ?>
  <ul>
  <?php
  $postargs_parent = array(
    'post_type' => '投稿タイプ名',
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomyName,
        'field' => 'slug',
        'terms' => $term->slug
      )
    )
  );
  $postslist_parent = get_posts( $postargs_parent );
  foreach ( $postslist_parent as $post ) : setup_postdata( $post ); ?>
    <li>
      <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
    </li>
  <?php
  endforeach;
  echo "</ul>";
  wp_reset_postdata();
  endif; //子タームが設定されていない場合終わり

  //親タームのIDから子タームの情報を出力
  $parentId = $term->term_id;
  $childargs = array(
    'parent' => $parentId,
    'hide_empty' => true
  );
  $childterms = get_terms($taxonomyName,$childargs);
  foreach ($childterms as $childterm) {
    $targetSlug = $childterm->slug; ?>
    <h3 id="<?php echo $targetSlug; ?>"><?php echo $childterm->name; ?></h3>
    <ul>
    <?php
    $postargs = array(
      'post_type' => '投稿タイプ名',
      'posts_per_page' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => $taxonomyName,
          'field' => 'slug',
          'terms' => $targetSlug
        )
      )
    );
    $postslist = get_posts( $postargs );
    foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
      </li>
    <?php
    endforeach;
    echo "</ul>";
    wp_reset_postdata();
  }
}
?>

親ターム名は”h2″、子ターム名は”h3″、記事一覧は”li”で出力されます。子タームが設定されていない場合は、親タームの見出しの下に親タームに属する記事が表示され、子タームが設定されている場合は、子タームの見出しの下に子タームに属する記事が表示されます。

「タクソノミー名」の部分と「投稿タイプ名」の部分を変更してください。また、記事が存在しないタームも出力したい場合は、’hide_empty’の値をfalseに変更します。

指定したタクソノミーの親ターム・子タームのリストと子タームに属する記事の一覧を出力する方法

親ターム・子タームのリストと子タームに属する記事のみを一覧表示させたい場合、テーマファイル内で以下のように記述します。


<?php
  $taxonomyName = "タクソノミー名";
  $args = array(
    'parent' => 0
  );
  $terms = get_terms($taxonomyName,$args);
  foreach ($terms as $term) { ?>
    <h2 id="<?php echo $term->slug; ?>"><?php echo $term->name; ?></h2>
    <?php
    $parentId = $term->term_id;
    $childargs = array(
      'parent' => $parentId,
      'hide_empty' => true
    );
    $childterms = get_terms($taxonomyName,$childargs);
    foreach ($childterms as $childterm) {
      $targetSlug = $childterm->slug; ?>
        <h3 id="<?php echo $targetSlug; ?>"><?php echo $childterm->name; ?></h3>
        <ul>
      <?php
      $postargs = array(
        'post_type' => '投稿タイプ名',
        'posts_per_page' => -1,
        'tax_query' => array(
          array(
            'taxonomy' => $taxonomyName,
            'field' => 'slug',
            'terms' => $targetSlug
          )
        )
      );
      $postslist = get_posts( $postargs );
      foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
        <li>
          <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
        </li>
      <?php
      endforeach;
      echo "</ul>";
      wp_reset_postdata();
    }
  }
?>

上記のように記述することで、親タームのみが設定されている記事は除外され、子タームが設定されている記事の一覧のみが出力されます。

taxonomy.phpなどで表示しているタームの情報を使って、親ターム・子タームのリストおよびそれぞれに属する投稿一覧を出力する方法

上述したコードでは、指定したタクソノミーのすべての親ターム・子タームのリストが出力されます。taxonomy.phpなどで表示しているページの親タームに属している記事一覧と子タームに属している記事一覧を出力したい場合は、以下のように親タームも指定してあげます。


<?php
  $taxonomyName = "カスタムタクソノミー名";
  $term_object = get_queried_object();
  $args = array(
    'parent' => 0,
    'slug' => $term_object->slug
  );
  $terms = get_terms($taxonomyName,$args);
  foreach ($terms as $term) {
    $children = get_terms($taxonomyName,'hierarchical=0&parent='.$term->term_id); if(!$children): //子タームが設定されていない場合 ?>
    <h2 id="<?php echo $term->slug; ?>"><?php echo $term->name; ?></h2>
    <ul>
    <?php
    $postargs_parent = array(
      'post_type' => '投稿タイプ名',
      'posts_per_page' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => $taxonomyName,
          'field' => 'slug',
          'terms' => $term->slug
        )
      )
    );
    $postslist_parent = get_posts( $postargs_parent );
    foreach ( $postslist_parent as $post ) : setup_postdata( $post ); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
      </li>
    <?php
    endforeach;
    echo "</ul>";
    wp_reset_postdata();
    endif; //子タームが設定されていない場合終わり

    //親タームのIDから子タームの情報を出力
    $parentId = $term->term_id;
    $childargs = array(
      'parent' => $parentId,
      'hide_empty' => true
    );
    $childterms = get_terms($taxonomyName,$childargs);
    foreach ($childterms as $childterm) {
      $targetSlug = $childterm->slug; ?>
      <h3 id="<?php echo $targetSlug; ?>"><?php echo $childterm->name; ?></h3>
      <ul>
      <?php
      $postargs = array(
        'post_type' => '投稿タイプ名',
        'posts_per_page' => -1,
        'tax_query' => array(
          array(
            'taxonomy' => $taxonomyName,
            'field' => 'slug',
            'terms' => $targetSlug
          )
        )
      );
      $postslist = get_posts( $postargs );
      foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
        <li>
          <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
        </li>
      <?php
      endforeach;
      echo "</ul>";
      wp_reset_postdata();
    }
  }
?>

get_queried_object()で表示しているページのターム情報を取得し、「’slug’ => $term_object->slug」でスラッグを指定しています。これにより、表示しているページの親タームのみが取得され、その子タームの情報のみが出力されるようになります。

子タームが設定されていない場合、親ターム名とその親タームに属する記事一覧が表示され、子タームが設定されている場合は、子ターム名とその子タームに属する記事の一覧が表示されます。

taxonomy.phpなどで表示しているタームの情報を使って、親ターム・子タームのリストおよび子タームに属する投稿一覧を出力する方法

taxonomy.phpなどで、表示しているページ(親ターム)の子タームに属している記事一覧を出力する場合は、以下のように記述します。


<?php
  $taxonomyName = "カスタムタクソノミー名";
  $term_object = get_queried_object();
  $args = array(
    'parent' => 0,
    'slug' => $term_object->slug
  );
  $terms = get_terms($taxonomyName,$args);
  foreach ($terms as $term) {
    $parentId = $term->term_id;
    $childargs = array(
      'parent' => $parentId,
      'hide_empty' => true
    );
    $childterms = get_terms($taxonomyName,$childargs);
    foreach ($childterms as $childterm) {
      $targetSlug = $childterm->slug; ?>
        <h3 id="<?php echo $targetSlug; ?>"><?php echo $childterm->name; ?></h3>
        <ul>
      <?php
      $postargs = array(
        'post_type' => '投稿タイプ名',
        'posts_per_page' => -1,
        'tax_query' => array(
          array(
            'taxonomy' => $taxonomyName,
            'field' => 'slug',
            'terms' => $targetSlug
          )
        )
      );
      $postslist = get_posts( $postargs );
      foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
        <li>
          <a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
        </li>
      <?php
      endforeach;
      echo "</ul>";
      wp_reset_postdata();
    }
  }
?>

上記のように記述することで、親タームのみが設定されている記事は除外されます。

あとがき

ループ処理が入れ子になっていて少々ややこしいですが、親ターム・子タームのリストとそれぞれのタームに紐づく投稿一覧を出力することができます。ぜひ参考にしただければと思います。

この記事が気に入ったら
いいね!してね♪

Twitter で
スポンサードリンク

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です