WordPress にパンくずリストを表示させる鉄板プラグイン Breadcrumb NavXT 。
パンくずリストって意外と自力実装するのが面倒ですよね。
プラグインを使いたくなる気持ちもわかります。
しかし、パンくずリストの設定なんてそう頻繁に変更するものではありません。
管理画面から変更できる必要がないのであれば、テーマのカスタマイズで実装したほうが高速です。
パンくずリストの実装については既に「WordPressサイトにパンくずリストをプラグインなしで実装する」で解説していますので、各テンプレートの記述の変更が苦でなければ、この記事のとおりでも問題ありません。
Breadcrumb NavXT を使っている場合、テンプレートに以下のように記述していると思います。
1 2 3 4 5 6 |
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="https://schema.org/"> <?php if(function_exists('bcn_display')) { bcn_display(); }?> </div> |
もし、プラグインを停止しても、この記述を変更したくなければ、以下のようにします。
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
<?php class My_Breadcramb { //パンくずの起点部分のトップページへのリンク。不要な場合は空白に。 const HOME_LABEL = "HOME"; //個別投稿ページのパンくずに表示するタクソノミー。不要な場合は空白もしくは項目削除。 const SHOW_TAXONOMIES = array( 'post' => 'category', 'products' => 'genre' ); private $paths = array(); private $post = null; private $term = null; private $post_type = null; public function __construct() { $this->prepare(); } /** * パンくずに使う情報を取得して配列に格納 */ public function prepare() { //まずは必要なオブジェクトを取得 if ( is_singular() ) { //個別ページ global $post; $this->post = $post; //標準投稿と固定ページ以外なら投稿タイプを取得 if ( $post->post_type != 'post' && $post->post_type != 'page' ) { $this->post_type = get_post_type_object( $post->post_type ); } //タクソノミーを使用する場合は、1番新しいタームを取得 if ( ! empty( self::SHOW_TAXONOMIES[$post->post_type] ) ) { $terms = get_the_terms( $post->ID, self::SHOW_TAXONOMIES[$post->post_type] ); if ( $terms ) { foreach ( $terms as $term ) { if ( ! $this->term || $this->term->id < $term->id ) { $this->term = $term; } } } } } elseif ( is_tax() || is_category() ) { //タクソノミーアーカイブ $this->term = get_queried_object(); //標準投稿と固定ページ以外なら投稿タイプを取得 $tax = get_taxonomy( $this->term->taxonomy ); $post_type = get_post_type_object( $tax->object_type[0] ); if ( $post_type->name != 'post' && $post_type->name != 'page' ) { $this->post_type = $post_type; } } elseif ( is_post_type_archive() ) { //投稿タイプアーカイブ $this->post_type = get_queried_object(); } //末端から順番に配列に入れていく $paths = array(); if ( $this->post ) { //親投稿も遡っていく $cursor = $this->post; while ( $cursor ) { $paths[] = array( 'label' => $cursor->post_title, 'link' => get_permalink( $cursor ) ); $cursor = $cursor->post_parent ? get_post( $cursor->post_parent ) : null; } } if ( $this->term ) { //親タームも遡っていく $cursor = $this->term; while ( $cursor ) { $paths[] = array( 'label' => $cursor->name, 'link' => get_term_link( $cursor ) ); $cursor = $cursor->parent ? get_term( $cursor->parent, $cursor->taxonomy ) : null; } } if ( $this->post_type ) { $paths[] = array( 'label' => $this->post_type->labels->name, 'link' => get_post_type_archive_link( $this->post_type->name ) ); } if (self::HOME_LABEL) { $paths[] = array( 'label' => self::HOME_LABEL, 'link' => home_url() ); } if ( $paths ) { //最初に入れたもの(=末端)はリンクをなくす $paths[0]['link'] = null; //順番をひっくり返す $this->paths = array_reverse( $paths ); } } /** * パンくず表示 */ public function display() { if ( $this->paths && count( $this->paths ) > 1 ) { $output = ''; foreach ( $this->paths as $index => $cramb ) { $output .= '<span property="itemListElement" typeof="ListItem">'; if ( $cramb['link'] ) { $output .= '<a property="item" typeof="WebPage" href="' . $cramb['link'] . '">'; } $output .= '<span property="name">' . $cramb['label'] . '</span>'; if ( $cramb['link'] ) { $output .= '</a>'; } $output .= '<meta property="position" content="' . ($index + 1) . '" />'; $output .= '</span>'; } echo $output; } } } |
1 2 3 4 5 6 7 |
if ( ! function_exists( 'bcn_display' ) ) { function bcn_display() { require_once( TEMPLATEPATH . '/includes/class-my-breadcramb.php' ); $breadcramb = new My_Breadcramb(); $breadcramb->display(); } } |
Breadcram NavXTに合わせて、microdata 記法から RDFa 記法に変えています。
また、li タグを span タグに変更し、一番外側のタグを出力しないようにしています。
そして、bcn_display() 関数の機能をのっとってしまいます。
・・・まぁ、テンプレートを見た人が勘違いしてしまう可能性が高いので、おとなしくテンプレートごと書き換えていただく方がベターだと思います。
なお、Breadcram NavXT の出力を、テンプレートタグではなくウィジェットで行っている場合、上記の方法では対応できません。
プラグインを停止し、ウィジェットを削除した後、「WordPressサイトにパンくずリストをプラグインなしで実装する」を参考に、テーマファイルを修正してください。