サポート » 使い方全般 » 親子タームのパンくずリスト

  • 親子タームのパンくずリストを作っています。
    子タームの記事一覧を表示してる場合、トップ>ブログ>親ターム(リンク付き)>子ターム(リンク無し)。
    シングルページでは、トップ>ブログ>親カテゴリー(リンク付き)>子ターム(リンク付き)>記事名。
    にしたいと思っています。
    以下のコードでは理想の形にはなっています。

    <ol>
    <li><a href="<?php echo bloginfo('url'); ?>">ホーム</a></li>
    <?php global $post;
    if(is_single() || is_tax() || is_date()):?>
    <li><a href="<?php echo bloginfo('url'); ?>/blog"><?php echo esc_html(get_post_type_object(get_post_type())->label); ?></a></li>
    <?php else: ?>
    <li><?php echo esc_html(get_post_type_object(get_post_type())->labels->singular_name ); ?></li>
    <?php endif; ?>
    
    <?php if(is_tax()):?>
    <?php
    $terms = wp_get_object_terms( $post->ID, 'blog_category');
    $term_id = $terms[0]->term_id;
    $ancestors = get_ancestors( $term_id, 'blog_category' );
    $reversed_ancestors = array_reverse($ancestors); 
    foreach($reversed_ancestors as $ancestor){
    echo '<li><a href="'. get_term_link($ancestor) .'">'. get_term($ancestor)->name .'</a></li>';
    }
    ?>
    <li><?php echo $wp_query ->get_queried_object() ->name;?></li>
    <?php endif; ?>
    	
    <?php if(is_single()): ?>		
    <?php
    $terms = wp_get_object_terms( $post->ID, 'blog_category');
    $term_id = $terms[0]->term_id;
    $ancestors = get_ancestors( $term_id, 'blog_category' );
    $reversed_ancestors = array_reverse($ancestors); 
    foreach($reversed_ancestors as $ancestor){
    echo '<li><a href="'. get_term_link($ancestor) .'">'. get_term($ancestor)->name .'</a></li>';
    }
    ?>
    <?php
    $terms = get_the_terms($post->ID, 'blog_category');
    foreach($terms as $term) {
    echo '<li><a href="'.get_term_link($term->slug, $term->taxonomy).'">'.$terms[0]->name.'</a></li>';
    }
    ?>
    <li><?php the_title(''); ?></li>
    <?php endif; ?>
    </ol>

    ですが、以下のコードで親子カテゴリーの一覧を取得できると思ったのですが、親タームしか表示されません。どう変更したら、自動で親ターム>子ターム>孫タームのように取得できるでしょうか?

    <?php
    $terms = wp_get_object_terms( $post->ID, 'blog_category');
    $term_id = $terms[0]->term_id;
    $ancestors = get_ancestors( $term_id, 'blog_category' );
    $reversed_ancestors = array_reverse($ancestors); 
    foreach($reversed_ancestors as $ancestor){
    echo '<li><a href="'. get_term_link($ancestor) .'">'. get_term($ancestor)->name .'</a></li>';
    }
    ?>
    • このトピックはtoteteが4年、 4ヶ月前に変更しました。
    • このトピックはtoteteが4年、 4ヶ月前に変更しました。
  • トピック「親子タームのパンくずリスト」には新たに返信することはできません。