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

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

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

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

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

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

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

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

パンくず

microdata 形式のパンくずリスト

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

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

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

  1. <ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
  2. <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
  3. <a href="/" itemprop="item">
  4. <span itemprop="name">レクタス</span>
  5. </a>
  6. <meta itemprop="position" content="1" />
  7. </li>
  8. <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
  9. <a href="https://www.rectus.co.jp/archives/category/blog/htmlcss" itemprop="item">
  10. <span itemprop="name">HTML&amp;CSS</span>
  11. </a>
  12. <meta itemprop="position" content="2" />
  13. </li>
  14. <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
  15. <a href="" itemprop="item">
  16. <span itemprop="name">リッチリザルトの ロゴ への対応</span>
  17. </a>
  18. <meta itemprop="position" content="3" />
  19. </li>
  20. </ol>

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

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

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

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

複数カテゴリにも対応

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

複数種類のページに対応

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

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

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

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

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

  1. function getBC($cat_id, $inLevel) {
  2. $cat_list = array();
  3. $stBCMain = '';
  4. while ($cat_id) {
  5. $cat = get_category($cat_id);
  6. $cat_link = get_category_link($cat_id);
  7. array_unshift($cat_list, '<a href="' . $cat_link . '" itemprop="item"><span itemprop="name">' . $cat->name . '</span></a>');
  8. $cat_id = $cat->parent;
  9. }
  10. foreach($cat_list as $value){
  11. $stBCMain .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">'. $value .'<meta itemprop="position" content="' . $inLevel++ . '" /></li>';
  12. }
  13. return array($stBCMain, $inLevel);
  14. }
  15. function getCateBC($cat_id, $inLevel) {
  16. $cat_list = array();
  17. $stBCMain = '';
  18. while ($cat_id) {
  19. $cat = get_category($cat_id);
  20. $cat_link = get_category_link($cat_id);
  21. array_unshift($cat_list, '<a href="' . $cat_link . '" itemprop="item"><span itemprop="name">' . $cat->name . '</span></a>');
  22. if ($cat_id->category_parent == 0) { break; }
  23. $cat_id = $cat_id->category_parent;
  24. }
  25. foreach($cat_list as $value){
  26. $stBCMain .= '<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">'. $value .'<meta itemprop="position" content="' . $inLevel++ . '" /></li>';
  27. }
  28. return array($stBCMain, $inLevel);
  29. }
  30. function my_breadcrumb() {
  31. $inLevel = 1;
  32. $stBCHead = '<div><ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">';
  33. // ホーム > と表示
  34. $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>';
  35. // ページでの階層表示
  36. $stBCFoot = '</ol></div>';
  37. $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>';
  38. if (is_single()) {
  39. // 通常ページ
  40. $cat_id = 0;
  41. $cats = get_the_category();
  42. $cat_num = count($cats);
  43. for ($i = 0;$i < $cat_num;$i++) {
  44. if (isset($cats[$i]->term_id)) {
  45. $cat_id = $cats[$i]->term_id;
  46. } else {
  47. break;
  48. }
  49. list($stBCMain, $inNewLevel) = getBC($cat_id, $inLevel);
  50. $stBCCur = sprintf($stListE, get_the_title(), $inNewLevel);
  51. echo $stBCHead . $stBCMain . $stBCCur . $stBCFoot . "\n";
  52. }
  53. } else {
  54. $stBCMain = '';
  55. if (is_archive()) {
  56. // アーカイブページ
  57. $id = get_the_category();
  58. $id = $id[0];
  59. if (0 < $id->category_parent) {
  60. // カテゴリで親がある場合
  61. $id = get_category($id->category_parent);
  62. list($stBCMain, $inNewLevel) = getCateBC($id, $inLevel);
  63. } else {
  64. $inNewLevel = $inLevel;
  65. };
  66. $stBCCur = sprintf($stListE, get_the_archive_title(), $inNewLevel);
  67. } else if (is_search()) {
  68. $stBCCur = sprintf($stListE, "検索: " . get_search_query(), 2);
  69. } else if (is_404()) {
  70. $stBCCur = sprintf($stListE, "ページが見つかりません", 2);
  71. }
  72. echo $stBCHead . $stBCMain . $stBCCur . $stBCFoot . "\n";
  73. }
  74. }

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

使い方としては

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

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

  1. <?php
  2. my_breadcrumb();
  3. ?>

リッチリザルトテスト

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

 

Contact

ご質問等ありましたら,お手数ですが弊社の個人情報保護方針をお読み頂いた上でフォームからお願い致します.
※このページと無関係な内容のセールスはご遠慮ください.

 
   
*は必ずご記入ください。
1. ご質問,アドバイス等ございましたらお書きください.
お客様情報
*メールアドレス
会社名
*御名前
※姓名間には空白をお願いします。
*電話番号
※"-"で区切ってください。
 上記項目にご記入頂き、「確認画面へ進む」ボタンを一回だけクリックしてください。
次回、お客様情報を入力しないで済むよう、暗号化してクッキーに記憶する。
contact
Pagetop