WordPressのタームにて、下記のようなタームを作成し、各親タームを除いた子と孫のタームだけを階層を維持した状態で取得したいのですが、孫と子すべて同じ扱いで取得したしまい階層別にというのがうまくいかない状態です。
【親タームA】
L子タームA
L孫タームA
L孫タームA2
L子タームB
L孫タームB
【親タームB】
L子タームA
L孫タームA
L孫タームA2
L子タームB
L孫タームB
出力に使用しているコードは下記の通りです。
■taxonomy-booktype.php
検証用 <?php $taxonomy = 'booktype'; //タクソノミー名 $args = array( 'pad_counts' => true, 'hide_empty' => 0, 'parent' => 0 ); $terms = get_terms( $taxonomy , $args ); ?> <?php $term_object = get_queried_object(); $term_id = $term_object->term_id; $termchildren = get_term_children( $term_id, $taxonomy ); foreach ( $termchildren as $child ): //その中で子タームを回す $termC = get_term_by( 'id' , $child, $taxonomy ); //if( $termC->count != 0 ):?> <h2 style="font-size:16px;margin:10px 0;"><?php echo $termC->name; ?></h2> <?php $grandson_id = $termchildren->term_id; $grandsons = get_term_children( $grandson_id, $taxonomy ); foreach ( $grandsons as $grandson ): //その中で子タームを回す ?> <p style="font-size:12px;margin-bottbom: 10px;margin-left:40px;"><?php echo $termC->name; ?></p> <?php //endif; endforeach; endforeach; ?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。