ブログの階層が「HOME>ディレクトリ―」だけなので必ずしもパンくずリストは必要がないと思うけど。。。
1] functions.phpに記述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// パンくずリスト function breadcrumb(){ global $post; $str =''; if(!is_home()&&!is_admin()){ $str.= '<div class="breadCrumb"><span>'; $str.= '<a href="'. home_url() .'"><font color="#FF0000">HOME</font></a></span>'; 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.='<span>></span><span><a href="'. get_category_link($ancestor) .'">'. get_cat_name($ancestor) .'</a></span>'; } } $str.='<span>></span><span><a href="'. get_category_link($cat -> term_id). '">'. $cat-> cat_name . '</a></span>'; } elseif(is_page()) { if($post -> post_parent != 0 ){ $ancestors = array_reverse(get_post_ancestors( $post->ID )); foreach($ancestors as $ancestor){ $str.='<span>></span><span><a href="'. get_permalink($ancestor).'">'. get_the_title($ancestor) .'</a></span>'; } } else{ $str.='<span>></span><span>'. $post -> post_title .'</span>'; } } elseif(is_single()){ $posts = get_post($post->ID); $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 .= '<span>></span><span><a href="'. get_category_link($ancestor) .'">' . get_cat_name($ancestor) .'</a></span>'; } } $str.='<span>></span><span><a href="'. get_category_link($cat -> term_id). '">' . $cat-> cat_name . '</a></span><span>></span><span>'. $posts->post_title .'</span>'; } elseif(is_search()){ $str.='<span>></span><span>検索結果 : '. esc_html( get_search_query( false ) ) .'</span>'; } else{ $str.='<div>'. wp_title('', false) .'</div>'; } $str.='</div>'; } echo $str; } // アーカイブの余計なタイトルを削除 add_filter( 'get_the_archive_title', function ($title) { if ( is_category() ) { $title = single_cat_title( '', false ); } elseif ( is_tag() ) { $title = single_tag_title( '', false ); } elseif ( is_month() ) { $title = single_month_title( '', false ); } return $title; }); |
2] style.cssに記述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
.breadCrumb{ margin-top: 20px; font-size: 9em; color: #339966; // コンテンツ同士の間の余白 > span + span{ margin-left: 16px; @include sp { margin-left: 10px; }; } } |
この手の情報は沢山あって目移りしたけど、シンプルな感じで作ることができた。