まずはコードを表示して後から説明をしていきます。
if ( ! function_exists( 'custom_breadcrumb' ) ) {
function custom_breadcrumb( $wp_obj = null ) {
if ( is_home() || is_front_page() ) return false;
$wp_obj = $wp_obj ?: get_queried_object();
echo '<ul id="breadcrumb">'.
'<li>'.
'<span class="" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">'.
'<a href="'. home_url() .'" itemprop="url"><span itemprop="title">ホーム</span></a>'.
'</span>'.
'</li>';
if ( is_attachment() ) {
echo '<li><span itemprop="title">'. $wp_obj->post_title .'</span></li>';
} elseif ( is_single() ) {
$post_id = $wp_obj->ID;
$post_type = $wp_obj->post_type;
$post_title = $wp_obj->post_title;
if ( $post_type !== 'post' ) {
$the_tax = "";
$tax_array = get_object_taxonomies( $post_type, 'names');
foreach ($tax_array as $tax_name) {
if ( $tax_name !== 'post_format' ) {
$the_tax = $tax_name;
break;
}
}
echo '<li>'.
'<span class="breadcrumbs" itemscope itemtype="http://data-vocabulary.org/Breadcrumb">'.
'<a href="'. get_post_type_archive_link( $post_type ) .'" itemprop="url">'.
'<span itemprop="title">'. get_post_type_object( $post_type )->label .'</span>'.
'</a>'.
'</span>'.
'</li>';
} else {
$the_tax = 'category';
}
if ( $the_tax !== "" ) {
$child_terms = array();
$parents_list = array();
$terms = get_the_terms( $post_id, $the_tax );
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->parent !== 0 ) $parents_list[] = $term->parent;
}
foreach ( $terms as $term ) {
if ( ! in_array( $term->term_id, $parents_list ) ) $child_terms[] = $term;
}
$term = $child_terms[0];
if ( $term->parent !== 0 ) {
$parent_array = array_reverse( get_ancestors( $term->term_id, $the_tax ) );
foreach ( $parent_array as $parent_id ) {
$parent_term = get_term( $parent_id, $the_tax );
echo '<li>'.
'<a href="'. get_term_link( $parent_id, $the_tax ) .'" itemprop="url">'.
'<span itemprop="title">'. $parent_term->name .'</span>'.
'</a>'.
'</li>';
}
}
echo '<li>'.
'<a href="'. get_term_link( $term->term_id, $the_tax ). '" itemprop="url">'.
'<span itemprop="title">'. $term->name .'</span>'.
'</a>'.
'</li>';
}
}
echo '<li><span itemprop="title">'. $post_title .'</span></li>';
} elseif ( is_page() ) {
$page_id = $wp_obj->ID;
$page_title = $wp_obj->post_title;
if ( $wp_obj->post_parent !== 0 ) {
$parent_array = array_reverse( get_post_ancestors( $page_id ) );
foreach( $parent_array as $parent_id ) {
echo '<li>'.
'<a href="'. get_permalink( $parent_id ).'" itemprop="url">'.
'<span itemprop="title">'.get_the_title( $parent_id ).'</span>'.
'</a>'.
'</li>';
}
}
echo '<li><span itemprop="title">'. $page_title .'</span></li>';
} elseif ( is_post_type_archive() ) {
echo '<li><span itemprop="title">'. $wp_obj->label .'</span></li>';
} elseif ( is_date() ) {
$year = get_query_var('year');
$month = get_query_var('monthnum');
$day = get_query_var('day');
if ( $day !== 0 ) {
//日別archive
echo '<li><a href="'. get_year_link( $year ).'"><span>'. $year .'年</span></a></li>'.
'<li><a href="'. get_month_link( $year, $month ). '"><span>'. $month .'月</span></a></li>'.
'<li><span>'. $day .'日</span></li>';
} elseif ( $month !== 0 ) {
//月別archive
echo '<li><a href="'. get_year_link( $year ).'"><span>'.$year.'年</span></a></li>'.
'<li><span>'.$month . '月</span></li>';
} else {
//年別archive
echo '<li><span>'.$year.'年</span></li>';
}
} elseif ( is_author() ) {
echo '<li><span>'. $wp_obj->display_name .' の執筆記事</span></li>';
} elseif ( is_archive() ) {
$term_id = $wp_obj->term_id;
$term_name = $wp_obj->name;
$tax_name = $wp_obj->taxonomy;
if ( $wp_obj->parent !== 0 ) {
$parent_array = array_reverse( get_ancestors( $term_id, $tax_name ) );
foreach( $parent_array as $parent_id ) {
$parent_term = get_term( $parent_id, $tax_name );
echo '<li>'.
'<a href="'. get_term_link( $parent_id, $tax_name ) .'" itemprop="url">'.
'<span itemprop="title">'. $parent_term->name .'</span>'.
'</a>'.
'</li>';
}
}
echo '<li>'.
'<span itemprop="title">'. $term_name .'</span>'.
'</li>';
} elseif ( is_search() ) {
echo '<li><span itemprop="title">「'. get_search_query() .'」で検索した結果</span></li>';
} elseif ( is_404() ) {
echo '<li><span>お探しの記事は見つかりませんでした。</span></li>';
} else {
echo '<li><span itemprop="title">'. get_the_title() .'</span></li>';
}
echo '</ul>';
}
}
はい終わりです。
表示させたいところに、
<?php custom_breadcrumb(); ?>
でOKです。
コードを順に追っていけばわかると思います。
こちらはmicrodataという方式でマークアップしているのですが、
JSON-LDでやっているパターンの記事もありました。お好きな方でやってみてください。
個人的にはパンくずリストはmicrodataの方がコードがまとまって綺麗な気がします。人それぞれですが。
他にもブログ記事の構造化のマークアップの記事を載せておきます。