wp.Vicunaにパンくずリスト設置

wp.Vicuna 1.5.9のパンくずリストはカスタム投稿タイプに対応していないので新たにパンくずリスト(breadcrumb/topicpath)を設置します。

すべてのページで使えるように作ってみました。

使い方

以下のコードをfunctions.phpに追加する。

require_once(dirname(__FILE__).'/breadcrumb.php');

breadcrumb.phpをテーマフォルダにアップロード。

テンプレートのお好みの位置に<?php breadcrumb(); ?>を追加。

breadcrumb.php

[sourcecode language="php"]<!--?php function breadcrumb($divOption = array("class" =--> "topicPath")){
wp_reset_query();

$get_link_html = function ($url, $title) {
return '<a href="'.$url.'">'.$title.'</a>';
};
$get_current_html = function ($title) {
return '<span>'.$title.'</span>';
};

$s_str = '?>?';

global $post;
$str ='';
if(!is_admin()){ /* !is_admin は管理ページ以外という条件分岐 */
$tagAttribute = '';
foreach($divOption as $attrName => $attrValue){
$tagAttribute .= sprintf(' %s="%s"', $attrName, $attrValue);
}
$str.= '<p'. $tagAttribute .'>';
$str.= $get_link_html(home_url(),'Home');
$cpt = get_post_types(array('_builtin' => false));
if(is_front_page()){

} elseif(is_home()){
$str.=$s_str;
$str.= $get_current_html(wp_title('',false));
} elseif (is_singular($cpt) && $cpt!=FALSE) { //カスタム投稿の個別記事ページ
$pt=get_post_type();
$str.=$s_str;
$str.= $get_link_html(get_post_type_archive_link($pt), get_post_type_object($pt)->label);
$str.=$s_str;
$str.= $get_current_html(get_the_title());
} elseif(is_category()){ //カテゴリーのアーカイブページ
$cat = get_queried_object();
if($cat -> parent != 0){
$ancestors = array_reverse(get_ancestors( $cat -> cat_ID, 'category' ));
foreach($ancestors as $ancestor){
$str.= $s_str;
$str.=$get_link_html(get_category_link($ancestor),get_cat_name($ancestor));
}
}
$str.=$s_str;
$str.= $get_current_html($cat -> name);
} 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.= $s_str;
$str.=$get_link_html(get_category_link($ancestor),get_cat_name($ancestor));
}
}
$str.= $s_str;
$str.= $get_link_html(get_category_link($cat -> term_id),$cat-> cat_name);
$str.= $s_str;
$str.= $get_current_html($post -> post_title);
} elseif(is_page()){ //固定ページ
if($post -> post_parent != 0 ){
$ancestors = array_reverse(get_post_ancestors( $post->ID ));
foreach($ancestors as $ancestor){
$str.= $s_str;
$str.=$get_link_html(get_permalink($ancestor),get_the_title($ancestor));
}
}
$str.= $s_str;
$str.= $get_current_html($post -> post_title);
} elseif(is_attachment()){ //添付ファイルページ
$str.= $s_str;
$str.= $get_current_html($post -> post_title);
} elseif(is_date()){ /* 日付アーカイブ */
$year=get_query_var('year');
$monthnum=get_query_var('monthnum');
$day=get_query_var('day');
if(is_day()){ /* 日別アーカイブ */
$str.= $s_str;
$str.= $get_link_html(get_year_link($year),get_post_time('Y'));
$str.= $s_str;
$str.= $get_link_html(get_month_link($year, $monthnum),get_post_time('F'));
$str.= $s_str;
$str.= $get_current_html(get_post_time('js'));
} elseif(is_month()){ /* 月別アーカイブ */
$str.= $s_str;
$str.= $get_link_html(get_year_link($year),get_post_time('Y'));
$str.= $s_str;
$str.= $get_current_html(get_post_time('F'));
} elseif(is_year()){ /* 年別アーカイブ */
$str.= $s_str;
$str.= $get_current_html(get_post_time('Y'));
}
} elseif(is_search()){ //検索結果表示ページ
$str.= $s_str;
$str.= $get_current_html('「'.get_search_query().'」で検索した結果');
} elseif(is_author()){ //投稿者のアーカイブページ
$author= get_query_var('author');
$str.= $s_str;
$str.=$get_current_html('投稿者 : '. get_the_author_meta('display_name', $author));
} elseif(is_tag()){ //タグのアーカイブページ
$str.= $s_str;
$str.= $get_current_html('タグ : '. single_tag_title( '' , false ));
} elseif(is_404()){ //404 Not Found ページ
$str.= $s_str;
$str.= $get_current_html('404 Not found');
} elseif ( is_post_type_archive() ) {
$posttype = get_post_type();
$str.= $s_str;
$str.= $get_current_html(post_type_archive_title('None', false));
} else{ //その他
$str.= $s_str;
$str.= $get_current_html(wp_title('',false));
}
$str.= '

';
}
echo $str;
// クエリをリセット
wp_reset_query();
}
?>[/sourcecode]

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です