この記事は2016年4月2日のものです。現在は状況が異なる可能性がありますのでご注意ください。
目次に行く・戻る
メルカリ招待コード: 招待コードをコピー
WordPressでブログを始めてからずっとなんとかならないかなぁ。 と思っていたことがあります。
それは、 パンくずリストが複数のカテゴリを選択して投稿した際には、 英数字(A~Z)から優先されて日本語(五十音順)の順になってしまい、 思っていたカテゴリのパンくずが表示されないです。
これまでもいろいろと試してみましたが、 これが今のところ一番かなぁ。
と思うので備忘録として残しておきたいと思います。
とても助けてくれたサイト(参考サイト)
アドセンス336pxPC閲覧記事下表示1つ目コード
+これまでによく読まれている記事一覧クリックでOPEN+
150,450件の PV
106,481件の PV
75,738件の PV
67,234件の PV
43,811件の PV
40,608件の PV
38,212件の PV
37,255件の PV
36,559件の PV
33,192件の PV
具体的な方法です。 function.php内に以下のコードを記載します。
一気に書き込みます。 (上記参考サイトさんのコードそのものだとliやulがあるので出力後に縦表示になってしまう為、ul、liを省いています。)
//パンくずリスト表示対象カテゴリをカスタムフィールドに追加追加するコードここから
add_action('admin_menu', 'add_breadcrumbs_category_meta_box');
add_action('save_post', 'save_breadcrumbs_category_custom_fields');
// パンくずリスト用メタボックスの追加
function add_breadcrumbs_category_meta_box() {
add_meta_box( 'my_sectionid4', 'パンくずリスト用カテゴリ', 'breadcrumbs_category_custom_fields', 'post', 'advanced' );
}
// パンくずリストカテゴリ選択用カスタムフィールドの入力フォーム作成と値の設定
function breadcrumbs_category_custom_fields() {
global $post;
$options = get_the_category($post->ID);
$n = count($options);
$radio_field = get_post_meta($post->ID,'_organizer_breadcrumbs_category',true);
for ($i=0; $i<$n; $i++) {
$option = $options[$i];
if ($option -> cat_ID == $radio_field) {
echo '<input type="radio" name="_organizer_breadcrumbs_category" value="'.esc_html($option->cat_ID).'" checked /> '.$option->cat_name.' ';
} else {
echo '<input type="radio" name="_organizer_breadcrumbs_category" value="'.esc_html($option->cat_ID).'" /> '.$option->cat_name.' ';
}
}
}
// パンくずリスト用カテゴリのカスタムフィールドの保存
function save_breadcrumbs_category_custom_fields( $post_id ) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$meta_key = '_organizer_breadcrumbs_category';
$meta_value_new = $_POST[$meta_key];
$meta_value_current = get_post_meta($post_id, $meta_key, true);
$cats = get_the_category($post_id);
if(!empty($cats)){
$meta_value_default = $cats[0]->cat_ID;
}
if(!empty($meta_value_current) ){
update_post_meta($post_id, $meta_key, $meta_value_new);
}else{
if(!empty($meta_value_new)){
update_post_meta($post_id, $meta_key, $meta_value_new);
}else{
if(!empty($meta_value_default)){
update_post_meta($post_id, $meta_key, $meta_value_default);
}
}
}
}
//パンくずリスト表示対象カテゴリをカスタムフィールドに追加追加するコードここまで
//パンくずリスト関数
//パンくずリストを出力する関数
function breadcrumb(){
global $post;
$str ='';
if(!is_home()&&!is_admin()){
$str.= '<div id="breadcrumb" class="clearfix">';
$str.= '';
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. home_url() .'/"><span itemprop="title"><strong>home</strong></span></a>';
$str.= '>';
if(is_search()){
$str.='「'. get_search_query() .'」で検索した結果';
} elseif(is_tag()){
$str.='タグ : '. single_tag_title( '' , false ). '';
} elseif(is_404()){
$str.='404 Not found';
} elseif(is_date()){
if(get_query_var('day') != 0){
$str.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_year_link(get_query_var('year')). '"><span itemprop="title">' . get_query_var('year'). '年</span></a>';
$str.='>';
$str.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_month_link(get_query_var('year'), get_query_var('monthnum')). '"><span itemprop="title">'. get_query_var('monthnum') .'月</span></a>';
$str.='>';
$str.=''. get_query_var('day'). '日';
} elseif(get_query_var('monthnum') != 0){
$str.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_year_link(get_query_var('year')) .'"><span itemprop="title">'. get_query_var('year') .'年</span></a>';
$str.='>';
$str.=''. get_query_var('monthnum'). '月';
} else {
$str.=''. get_query_var('year') .'年';
}
} 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.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_category_link($ancestor) .'"><span itemprop="title">'. get_cat_name($ancestor) .'</span></a>';
$str.='>';
}
}
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $cat -> name .'</span>';
} elseif(is_author()){
$str .='投稿者 : '. get_the_author_meta('display_name', get_query_var('author')).'';
} elseif(is_page()){
if($post -> post_parent != 0 ){
$ancestors = array_reverse(get_post_ancestors( $post->ID ));
foreach($ancestors as $ancestor){
$str.='itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_permalink($ancestor).'"><span itemprop="title">'. get_the_title($ancestor) .'</span></a>';
$str.='>';
}
}
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $post -> post_title .'</span>';
} elseif(is_attachment()){
if($post -> post_parent != 0 ){
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_permalink($post -> post_parent).'"><span itemprop="title">'. get_the_title($post -> post_parent) .'</span></a>';
$str.='>';
}
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_permalink($post -> ID).'"><span itemprop="title">' . $post -> post_title . '</span></a>';
} elseif(is_single()){
$cat_ID = get_post_meta($post->ID,'_organizer_breadcrumbs_category', true);
if($cat_ID){
$cat = get_category($cat_ID);
} else {
$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.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_category_link($ancestor).'"><span itemprop="title">'. get_cat_name($ancestor). '</span></a>';
$str.='>';
}
}
$str.='<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="'. get_category_link($cat -> term_id). '"><span itemprop="title">'. $cat-> cat_name . '</span></a>';
$str.='>';
$str.= '<itemscope itemtype="https://data-vocabulary.org/Breadcrumb"><span itemprop="title">'. $post -> post_title .'</span>';
} else{
$str.=''. wp_title('', false) .'';
}
$str.='';
$str.='</div>';
}
echo $str;
}
//パンくずリスト関数ここまで
その後、 パンくずリストを表示したい部分に以下のコードを記載します。
<?php breadcrumb(); ?>
この記載したところに意図して選択したパンくずリストが表示されます。
そして、 最後にCSSで調整しました。
#breadcrumb {
font-size: 13px;
color: #000
}
div#breadcrumb a {
color: #000;
}
最後に、 よくを言うなら親カテゴリとか子カテゴリとかじゃなくて、
選択したカテゴリをすべて順番にすべてパンくずリストに表示したいと思っています。
以下のサイトの様に…。
WordPressでも、 選択したカテゴリのすべてがパンくずリストとして表示できるようにならないかなぁ。
もしくは、 そんな方法を見つけるか作りたいなぁ…。
あなたにおすすめな関連記事 おすすめの記事一部広告 おすすめ!! 世界中でNo.1のシャアを誇るAmazonでのお得なお買い物でお得をしよう!!ネット通販なので、もちろん24時間年中無休です。健康商品から食品、車カスタマイズ商品、大手家電メーカー商品やパソコンの自作サプライ商品、その他アマゾン・プライムでは映画やドラマなど多くの作品や番組を見ることができます。マスクなど衛生商品も今では必須アイテム。一番信用できて一番商品数の多いAmazonでの買い物が絶対にオススメ!!
おすすめ2 楽天による季節特集です。それぞれの季節ごとにお得なお買い物商品やお得な旅行商品、その他にもAmazon同様に国内最大の買い物ポータルサイトです。楽天をメインにお買い物されている方はポイントやSPUも貯まりさらにお得に!!楽天カードや宇佐美系列のガソリンスタンドでも楽天ポイントが今では貯まります。それらのポイントを貯めてお得にお買い物や旅行商品その他にもパソコンサプライや車カスタマイズ商品など楽天も充実しています。
Total179
アドセンス336pxPC閲覧記事下表示1つ目コード
目次に戻る
-WordPress , カスタマイズ , カスタマイズ , 伝えたいこと , 備忘録 -パンくずリスト
目次に戻る
目次に戻る
Opt-out complete; your visits to this website will not be recorded by the Web Analytics tool. Note that if you clear your cookies, delete the opt-out cookie, or if you change computers or Web browsers, you will need to perform the opt-out procedure again.
You may choose to prevent this website from aggregating and analyzing the actions you take here. Doing so will protect your privacy, but will also prevent the owner from learning from your actions and creating a better experience for you and other users.
This opt out feature requires JavaScript.
The tracking opt-out feature requires cookies to be enabled.
comment