WordPressでブログを始めてからずっとなんとかならないかなぁ。
と思っていたことがあります。
それは、
パンくずリストが複数のカテゴリを選択して投稿した際には、
英数字(A~Z)から優先されて日本語(五十音順)の順になってしまい、
思っていたカテゴリのパンくずが表示されないです。
これまでもいろいろと試してみましたが、
これが今のところ一番かなぁ。
と思うので備忘録として残しておきたいと思います。
車メンテナンス・カスタマイズ・点検・修理、WordPressカスタマイズ、PC設定、車、時事ネタ、情勢、日々の出来事など津々浦々に綴っていきます。
WordPress カスタマイズ カスタマイズ 伝えたいこと 備忘録
2025年8月16日 (土曜日)本日の人気記事トップ10
所要時間目安: 約 7 分
今日は2025年8月16日です。
この記事は2016年4月2日のものです。現在は状況が異なる可能性がありますのでご注意ください。
2025年8月16日 (土曜日)↓における週間月間人気記事トップ3は?↓
計測データがありません。
計測データがありません。
計測データがありません。
WordPressでブログを始めてからずっとなんとかならないかなぁ。
と思っていたことがあります。
それは、
パンくずリストが複数のカテゴリを選択して投稿した際には、
英数字(A~Z)から優先されて日本語(五十音順)の順になってしまい、
思っていたカテゴリのパンくずが表示されないです。
これまでもいろいろと試してみましたが、
これが今のところ一番かなぁ。
と思うので備忘録として残しておきたいと思います。
アドセンス336pxPC閲覧記事下表示1つ目コード
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(); ?>
この記載したところに意図して選択したパンくずリストが表示されます。
#breadcrumb {
font-size: 13px;
color: #000
}
div#breadcrumb a {
color: #000;
}
最後に、
よくを言うなら親カテゴリとか子カテゴリとかじゃなくて、
以下のサイトの様に…。
WordPressでも、
選択したカテゴリのすべてがパンくずリストとして表示できるようにならないかなぁ。
もしくは、
そんな方法を見つけるか作りたいなぁ…。
人気の商品が日替わりで登場。毎日お得なタイムセール「Amazonタイムセール全体」
先日公開させていただいた記事(:AMP化する方法プラグインなしで対応してみる)でWordPressにおいてプラグインを使用しないでなんとかAMP対応できないかなぁ。と思い試行錯誤中です。 先日の状態で...
先月末くらいだったでしょうか…。ポストにタイトルにありますように「テレビの映像が乱れる可能性があります。」って大きく書かれているチラシが入っていました。要は、これまで携帯電話(スマホ)で利用していた8...
(アイキャッチ画像はhttps://news.tbs.co.jp/newseye/tbs_newseye2826318.htmの動画のスクリーンショットです。) 昨日(2016年7月20日)一部メディ...
先日の記事として、以下の記事を投稿しましたが、逆にAdblock Plus側もその広告ブロック遮断に対して対策をとあります。 どちらの言い分もあるのかもしれませんが、一応You Tubeの利用規約には...
Multiplex 広告
あなたが訪問してから
⏰ 0 秒経過 🎉ホワイトデープレゼントは以下などの豊富なキャンペーン商品から選ぶと良いと思います。
今週の人気記事トップ10
アドセンス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.
The tracking opt-out feature requires cookies to be enabled.
This site is protected by reCAPTCHA and the GooglePrivacy PolicyandTerms of Serviceapply.
このサイトはreCAPTCHAによって保護されており、Googleのプライバシーポリシーと利用規約が適用されます。
新着コメント