親カテゴリーの子カテゴリーのタイトル出力【カスタム投稿】

wordpress

<?php  // get_termsで特定タクソノミーのタームを取得してタームごとにforeach開始
    $target_post = '(投稿タイプのスラッグ)';
    $target_post_cat = '(タクソノミーのスラッグ)';
    $post_count = -1;

    $cat_args = array(
        'parent' => 0, //トップレベルのタームのみ
        'hierarchical' => 0 //子タームを含めない
    );

    $cats = get_terms($target_post_cat, $cat_args);

    foreach( $cats as $cat ):
?>

<?php  // 親タームタイトルを表示
    $target_cat_slug = esc_html($cat -> slug);
    $target_cat_name = esc_html($cat -> name);
?>
<h2><?php echo $target_cat_name; ?></h2>

<?php  //タームごとに子ターム取得(孫ターム・投稿記事を持たないタームを除外) 
    $child_cats = get_terms($target_post_cat,'hierarchical=0&hide_empty=1&parent='.$cat -> term_id); 
?>

<?php  //子タームがある場合の処理
    if( $child_cats ) : 
?>

<?php  //投稿記事を持つ子タームスラッグを除外用配列に格納(foreach内)
    foreach( $child_cats as $child_cat  ){
        $exclude[] =  esc_html($child_cat->slug);
    }
?>

<?php  //子タームを持つタームで、親タームにのみを設定した投稿のループ、除外用配列を使用
    $args = array(
        'post_type' => $target_post,
        'tax_query' => array( //ここからタクソノミーのパラメーター
            'relation' => 'AND',
            array(
                'taxonomy' => $target_post_cat, //カスタム分類(カスタムタクソノミー)
                'field' => 'slug', //タクソノミータームの種類をスラッグで指定する
                'terms' => $target_cat_slug //タームからスラッグ取得 
            ),
            array(
                'taxonomy' => $target_post_cat, //カスタム分類(カスタムタクソノミー)
                'field' => 'slug', //タクソノミータームの種類をスラッグで指定する
                'terms' => $exclude, //除外タームスラッグ取得 
                'operator' => 'NOT IN' //除外
            )
        ),
        'post_status' => 'publish',
        'posts_per_page' => $post_count, // 表示するページ数
        'orderby' => 'menu_order', // 必要な場合に指定
        'order' => 'DESC' // 並び順
    );

    $my_query = new WP_Query($args);
?>

<?php if( $my_query->have_posts() ) : ?>
<ul>
    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
<?php endif; ?>

<?php  //子タームを設定した投稿のループ(foreach内)
    foreach( $child_cats as $child_cat ): 
?>

<?php
    $child_cat_name = esc_html($child_cat -> name);
    $target_cat_slug = esc_html($child_cat->slug);
?>

<h3><?php echo $child_cat_name; ?></h3>

<?php
    $args = array(
        'post_type' => array($target_post),
        'taxonomy' => $target_post_cat,
        'term' => $target_cat_slug,
        'post_status' => 'publish',
        'posts_per_page' => $post_count, // 表示するページ数
        'orderby' => 'menu_order',
        'order' => 'DESC' // 並び順
    );

    $my_query = new WP_Query($args);
?>

<?php if( $my_query->have_posts() ) : ?>
<ul>
    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endforeach; ?>

<?php  //子タームがない場合の処理
 else:
?>

<?php
    $args = array(
        'post_type' => array($target_post),
        'taxonomy' => $target_post_cat,
        'term' => $target_cat_slug,
        'post_status' => 'publish',
        'posts_per_page' => $post_count, // 表示するページ数
        'orderby' => 'menu_order',
        'order' => 'DESC' // 並び順
    );

    $my_query = new WP_Query($args);
?>

<?php if( $my_query->have_posts() ) : ?>
<ul>
    <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endif; ?>

<?php  //$postをリセットしてforeach終了
    wp_reset_postdata(); ?>
<?php endforeach; ?>

こんな感じで出力されます。

<h2>ターム1</h2>
<h3>子ターム1</h3>
<ul>
<li><a href="">タイトル1</a></li>
<li><a href="">タイトル2</a></li>
<li><a href="">タイトル3</a></li>
</ul>

<h2>ターム2</h2>
<h3>子ターム2</h3>
<ul>
<li><a href="">タイトル4</a></li>
<li><a href="">タイトル5</a></li>
<li><a href="">タイトル6</a></li>
</ul>

<h2>ターム3</h2>
<h3>子ターム3</h3>
<ul>
<li><a href="">タイトル7</a></li>
<li><a href="">タイトル8</a></li>
<li><a href="">タイトル9</a></li>
</ul>

<h3>子ターム4</h3>
<ul>
<li><a href="">タイトル10</a></li>
<li><a href="">タイトル11</a></li>
<li><a href="">タイトル12</a></li>
</ul>