• 解決済 tri3shnyirl5a3f

    (@tri3shnyirl5a3f)


    Breadcrumb NavXTで、通常の投稿に紐付けた複数のタクソノミーを表示するにはどうすればよいでしょうか?
    ECサイトにあるような複数のパンくずを生成したいと考えています。また通常のカテゴリーだけだと、スラッグの命名ルールからURLの階層が限られてしまうため、タクソノミーを5つほど通常の投稿に追加している点が特殊かと思います。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • CG

    (@du-bist-der-lenz)

    ECサイトの商品ページのカテゴリー・リンクは、複数のパンくずを生成しているんですね。

    こちらのトピックが参考になると思います。
    https://ja.wordpress.org/support/topic/ネットショップのような複数のパンくずリストを/

    Breadcrumb NavXT プラグインのフックを駆使すればできないことはないかもしれませんが、ほぼまるまるパンくずリストを作成することになるので自作した方が早いと思います。

    タクソノミーをパンくずリスト風に表示する、サンプルコードを書いてみました。参考までに。

    テンプレートに、

    <div class="breadcrumbs">
    <?php if ( function_exists( 'bcn_display' ) ) { bcn_display(); } ?>
    </div>
    <div class="breadcrumbs-sub1">
    <?php echo get_the_breadcrumb_taxonomy( 'タクソノミー1' ); ?>
    </div>
    <div class="breadcrumbs-sub2">
    <?php echo get_the_breadcrumb_taxonomy( 'タクソノミー2' ); ?>
    </div>

    functions.php に、

    function get_the_breadcrumb_taxonomy( $taxonomy = 'category' ) {
    	$post_id = get_the_ID();
    
    	$breadcrumbs = array();
    
    	$breadcrumbs[] = array( 'title' => 'TOP', 'link' => home_url( '/' ) );
    
    	$terms = get_the_terms( $post_id, $taxonomy );
    	if ( $terms && ! is_wp_error( $terms ) ) {
    
    		$descendant_term = $terms[0];
    
    		if ( count( $terms ) > 1) {
    			foreach ( $terms as $key => $term ) {
    				foreach ( $terms as $term2 ) {
    					if ( term_is_ancestor_of( $term->term_id, $term2->term_id, $taxonomy ) ) {
    						$descendant_term = $term2;
    						break;
    					}
    				}
    			}
    		}
    
    		$term_ids = array_reverse( get_ancestors( $descendant_term->term_id, $taxonomy ) );
    		foreach ( $term_ids as $term_id ) {
    			$term = get_term( $term_id, $taxonomy );
    			$breadcrumbs[] = array( 'title' => $term->name, 'link' => get_term_link( $term_id ) );
    		}
    
    		$breadcrumbs[] = array( 'title' => $descendant_term->name, 'link' => get_term_link( $descendant_term->term_id ) );
    	}
    
    	$output = '';
    	foreach ( $breadcrumbs as $breadcrumb ) {
    		$output .= sprintf( '<span><a href="%1$s" title="%2$s"><span property="name">%2$s</span></a></span> > ',
    			esc_url( $breadcrumb['link'] ),
    			esc_html( $breadcrumb['title'] )
    		);
    	}
    	$output .= '<span>' . get_the_title() . '</span>';
    
    	return $output;
    }
    トピック投稿者 tri3shnyirl5a3f

    (@tri3shnyirl5a3f)

    >ishitaka
    コードまで書いていただきありがとうございます!これを参考に試してみます!

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「Breadcrumb NavXTで通常の投稿に紐付けたタクソノミーの表示」には新たに返信することはできません。