【WordPress】リッチリザルト対応の自動パンくずリスト生成

最終更新日: 公開日: 2021年01月

このところ,リッチリザルトの投稿が続いている.

そこで今更ではあるが「パンくずリスト」についても書いてみる.

折角なので WordPress を使っている場合に自動でパンくずリストを生成する方法を書いてみよう.

プラグインとしては Breadcrumb NavXT などがあるようだが,プラグインを入れるまでもないので作ってみた.Googleリッチリザルトにも対応します.
つまり,プラグインなしの自作の自動パンくずリスト作成機能ということになる.

javascript はあえて使っていない.javascript で作ると検索エンジンに目次を認識してもらえない可能性も考えてのことである.(最近は収集ロボットも javascript を実行している節があるので,そこまで気にする必要はないかもしれない.)

PHP でサーバーの方で作成しているので確実に検索エンジンにも認識してもらえる.

microdata 形式のパンくずリスト

まず,パンくずリストを microdata 方式で普通に書くと以下のようになる.

参考:パンくずリスト | Google 検索セントラル | Google Developers

前の記事の「リッチリザルトの ロゴ への対応」のパンくずリストは以下のようになっている.

<ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
  <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
    <a href="/" itemprop="item">
      <span itemprop="name">レクタス</span>
    </a>
    <meta itemprop="position" content="1" />
  </li>
  <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
    <a href="https://www.rectus.co.jp/archives/category/blog/htmlcss" itemprop="item">
      <span itemprop="name">HTML&amp;CSS</span>
    </a>
    <meta itemprop="position" content="2" />
  </li>
  <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
    <a href="" itemprop="item">
      <span itemprop="name">リッチリザルトの ロゴ への対応</span>
    </a>
    <meta itemprop="position" content="3" />
  </li>
</ol>

【WordPress】複数カテゴリ対応パンくずリスト自動生成サンプルコード

自動でパンくずリストを作るための関数の説明

以下の4つの関数を functions.php に挿入する.

getBC は通常ページのパンくずリストを作る関数
getCateBC はカテゴリページのパンくずリストを作る関数
my_breadcrumb はパンくずリストを表示する関数

複数カテゴリにも対応

折角スクリプトにするので,投稿した記事が複数のカテゴリに含まれる場合は複数のパンくずリストが表示されるようにしている.
このページのようにパンくずリストが縦に並ぶ.

複数種類のページに対応

投稿記事だけでなく,カテゴリページ,年月ページ,タグページ,検索結果ページ,404ページにも対応している.

カテゴリページに親子関係がある場合,それも表示するようにしている.

パンくずリスト自動生成サンプルコード

CSS は省いているので自サイトに応じて適宜適用してほしい.
以下の関数を functions.php に挿入.

プログラムを一部修正.(2021/02/04)
プログラムを修正.(2021/04/07)

function getBC($cat_id, $inLevel) {
  $cat_list = array();
  $stBCMain = '';
  while ($cat_id) {
    $cat = get_category($cat_id);
    $cat_link = get_category_link($cat_id);
    array_unshift($cat_list, '<a href="' . $cat_link . '" itemprop="item"><span itemprop="name">' . $cat->name . '</span></a>');
    $cat_id = $cat->parent;
  }
  foreach($cat_list as $value){
    $stBCMain .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">'. $value .'<meta itemprop="position" content="' . $inLevel++ . '" /></li>';
  }
  return array($stBCMain, $inLevel);
}
function getCateBC($cat_id, $inLevel) {
  $cat_list = array();
  $stBCMain = '';
  while ($cat_id) {
    $cat = get_category($cat_id);
    $cat_link = get_category_link($cat_id);
    array_unshift($cat_list, '<a href="' . $cat_link . '" itemprop="item"><span itemprop="name">' . $cat->name . '</span></a>');
    if ($cat_id->category_parent == 0) { break; }
    $cat_id = $cat_id->category_parent;
  }
  foreach($cat_list as $value){
    $stBCMain .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">'. $value .'<meta itemprop="position" content="' . $inLevel++ . '" /></li>';
  }
  return array($stBCMain, $inLevel);
}
function my_breadcrumb() {
  $inLevel = 1;
  $stBCHead = '<div><ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">';
  // ホーム > と表示
  $stBCHead .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/" itemprop="item"><span itemprop="name">ホーム</span></a><meta itemprop="position" content="' . $inLevel++ . '" /></li>';
  // ページでの階層表示
  $stBCFoot = '</ol></div>';
  $stListE = '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="" itemprop="item"><span itemprop="name">%s</span></a><meta itemprop="position" content="%s" /></li>';
  if (is_single()) {
    // 通常ページ
    $cat_id = 0;
    $cats = get_the_category();
    $cat_num = count($cats);
    for ($i = 0;$i < $cat_num;$i++) {
      if (isset($cats[$i]->term_id)) {
        $cat_id = $cats[$i]->term_id;
      } else {
        break;
      }
      list($stBCMain, $inNewLevel) = getBC($cat_id, $inLevel);
      $stBCCur = sprintf($stListE, get_the_title(), $inNewLevel);
      echo $stBCHead . $stBCMain . $stBCCur . $stBCFoot . "\n";
    }
  } else {
    $stBCMain = '';
    if (is_archive()) {
      // アーカイブページ
      $id = get_the_category();
      $id = $id[0];
      if (0 < $id->category_parent) {
        // カテゴリで親がある場合
        $id = get_category($id->category_parent);
        list($stBCMain, $inNewLevel) = getCateBC($id, $inLevel);
      } else {
        $inNewLevel = $inLevel;
      };
      $stBCCur = sprintf($stListE, get_the_archive_title(), $inNewLevel);
    } else if (is_search()) {
      $stBCCur = sprintf($stListE, "検索: " . get_search_query(), 2);
    } else if (is_404()) {
      $stBCCur = sprintf($stListE, "ページが見つかりません", 2);
    }
    echo $stBCHead . $stBCMain . $stBCCur . $stBCFoot . "\n";
  }
}

「ホーム」と書いてある部分は企業の場合は自社の名前にするとよいだろう.

使い方としては

  • single.php
  • category.php
  • date.php
  • tag.php
  • search.php
  • 404.php

に以下のような記述を挿入する.
挿入した場所にパンくずリストが表示される.

<?php
my_breadcrumb();
?>

リッチリザルトテスト

埋め込んだ後は下記URLでテストするのを忘れないようにする.
リッチリザルトテスト(google)

 

Contact

ご質問等ありましたら,お手数ですが弊社の個人情報保護方針をお読み頂いた上でフォームからお願い致します.

 
   
Chrome 拡張機能 PageSpeed Insights SEO WordPress Youtube お問い合わせフォーム アナリティクス アンケートフォーム サーチコンソール セミナー タグマネージャー データベース ヒアリング プラグイン ページ閲覧解析 マーケティング メール配信 リッチリザルト レスポンシブ対応 ログ解析 勉強会 商品企画 営業 営業力 営業支援システム 実装 検索順位 検索順位チェックツール 追跡 開発者ツール
contact
Pagetop