Skip to content

Instantly share code, notes, and snippets.

@apr20

apr20/header.php Secret

Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apr20/f83048c886d795cf3fd5 to your computer and use it in GitHub Desktop.
Save apr20/f83048c886d795cf3fd5 to your computer and use it in GitHub Desktop.
WordPressのパンくずリスト microdata対応版
<?php
function breadcrumb($divOption = array("id" => "breadcrumb", "class" => "clearfix")){
global $post;
$str ='';
if(!is_home()&&!is_admin()){ /* !is_admin は管理ページ以外という条件分岐 */
$tagAttribute = '';
foreach($divOption as $attrName => $attrValue){
$tagAttribute .= sprintf(' %s="%s"', $attrName, $attrValue);
}
$str.= '<div'. $tagAttribute .'>';
$str.= '<ul>';
$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. home_url() .'/" itemprop="url"><span itemprop="title">ホーム</span></a></li>';
$str.= '<li>&gt;</li>';
if(is_category()) { //カテゴリーのアーカイブページ
$cat = get_queried_object();
if($cat -> parent != 0){
$ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
foreach($ancestors as $ancestor){
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor) .'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor) .'</span></a></li>';
$str.='<li>&gt;</li>';
}
}
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> cat_ID) .'" itemprop="url"><span itemprop="title">'. $cat -> name . '</span></a></li>';
} elseif(is_single()){ //ブログの個別記事ページ
$categories = get_the_category($post->ID);
$cat = $categories[0];
if($cat -> parent != 0){
$ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
foreach($ancestors as $ancestor){
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($ancestor).'" itemprop="url"><span itemprop="title">'. get_cat_name($ancestor). '</span></a></li>';
$str.='<li>&gt;</li>';
}
}
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_category_link($cat -> term_id). '" itemprop="url"><span itemprop="title">'. $cat-> cat_name . '</span></a></li>';
$str.='<li>&gt;</li>';
$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</span></a></li>';
} elseif(is_page()){ //固定ページ
if($post -> post_parent != 0 ){
$ancestors = array_reverse(get_post_ancestors( $post->ID ));
foreach($ancestors as $ancestor){
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_permalink($ancestor).'" itemprop="url"><span itemprop="title">'. get_the_title($ancestor) .'</span></a></li>';
$str.='<li>&gt;</li>';
}
}
$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_permalink($post->ID).'" itemprop="url"><span itemprop="title">'. $post -> post_title .'</span></a></li>';
} elseif(is_date()){ //日付ベースのアーカイブページ
if(get_query_var('day') != 0){ //年別アーカイブ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')). '" itemprop="url"><span itemprop="title">' . get_query_var('year'). '年</span></a></li>';
$str.='<li>&gt;</li>';
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_query_var('year'), get_query_var('monthnum')). '" itemprop="url"><span itemprop="title">'. get_query_var('monthnum') .'月</span></a></li>';
$str.='<li>&gt;</li>';
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) .'" itemprop="url"><span itemprop="title">'. get_query_var('day'). '日</span></a></li>';
} elseif(get_query_var('monthnum') != 0){ //月別アーカイブ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')) .'" itemprop="url"><span itemprop="title">'. get_query_var('year') .'年</span></a></li>';
$str.='<li>&gt;</li>';
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_month_link(get_query_var('year'), get_query_var('monthnum')) .'" itemprop="url"><span itemprop="title">'. get_query_var('monthnum'). '月</span></a></li>';
} else { //年別アーカイブ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'. get_year_link(get_query_var('year')) .'" itemprop="url"><span itemprop="title">'. get_query_var('year') .'年</span></a></li>';
}
} elseif(is_search()) { //検索結果表示ページ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_bloginfo('url').'/?'.$_SERVER['QUERY_STRING'].'" itemprop="url"><span itemprop="title">「'. get_search_query() .'」の検索結果</span></a></li>';
} elseif(is_author()){ //投稿者のアーカイブページ
$str .='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_author_posts_url(get_query_var('author')).'" itemprop="url"><span itemprop="title">投稿者 : '. get_the_author_meta('display_name', get_query_var('author')).'</span></a></li>';
} elseif(is_tag()){ //タグのアーカイブページ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="'.get_tag_link(get_query_var('tag_id')).'" itemprop="url"><span itemprop="title">タグ : '. single_tag_title( '' , false ). '</span></a></li>';
} elseif(is_attachment()){ //添付ファイルページ
$str.= '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $post -> post_title .'</span></li>';
} elseif(is_404()){ //404 Not Found ページ
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">404 Not found</span></li>';
} else{ //その他
$str.='<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. wp_title('', true) .'</span></li>';
}
$str.='</ul>';
$str.='</div>';
}
echo $str;
}
?>
@apr20
Copy link
Author

apr20 commented Aug 12, 2014

このコードだと、WordPressのパーマリンク設定をデフォルトのまま運用すると「ホーム>0年」のようになってしまうので、修正版を投稿しました。
https://gist.github.com/apr20/a43b6203a61940d77ac7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment