EmDashでパンくずリストを作る:表示と構造化データ(BreadcrumbList)

EmDashでパンくずリストを作る手順です。共通パーツとして部品を1つ作り、記事のカテゴリーを読み込んで表示し、構造化データ(BreadcrumbList)を出力するところまでを、実際に動かして確かめました。

WordPressでは、パンくずリストをプラグインや自作の関数で出力しました。EmDashのテンプレートには、パンくずを出す専用の部品は見当たりません。共通パーツとして自分で作ります。

先に結論

  • パンくずの部品を1つ作り、表示したい項目を配列で渡します
  • カテゴリーの名前は取り出した記事のpost.data.terms?.categoryから読めます
  • 構造化データ(BreadcrumbList)は、同じ部品の中でJSON-LDとして出力します

手順1:パンくずの部品を作る

src/components/Breadcrumb.astro
---
interface Item {
	label: string;
	href?: string;
}

interface Props {
	items: Item[];
}

const { items } = Astro.props;
---

<nav aria-label="パンくずリスト">
	<ol class="breadcrumb">
		{items.map((item) => (
			<li>
				{item.href ? <a href={item.href}>{item.label}</a> : <span aria-current="page">{item.label}</span>}
			</li>
		))}
	</ol>
</nav>

見た目は次のCSSで付けています。

.breadcrumb {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5em;
	padding: 0;
	list-style: none;
	font-size: 0.875rem;
}

.breadcrumb li + li::before {
	content: ">";
	margin-right: 0.5em;
	color: #888;
}
  • hrefがある項目はリンクにし、ない項目(今のページ)は文字だけにしています
  • 区切りの「>」は、CSSで付けています。HTMLには書いていません

共通パーツの作り方の基本は、get_template_partにあたる「コンポーネント」の使い方で説明しています。

手順2:記事のページで使う

---
import { getEmDashEntry } from "emdash";
import Base from "../layouts/Base.astro";
import Breadcrumb from "../components/Breadcrumb.astro";

const { entry: post } = await getEmDashEntry("posts", "october-holidays");
if (!post) return Astro.rewrite("/404");

const category = post.data.terms?.category?.[0];
const items = [
	{ label: "ホーム", href: "/" },
	{ label: "お知らせ", href: "/posts" },
	...(category ? [{ label: category.label, href: `/category/${category.slug}` }] : []),
	{ label: post.data.title },
];
---

<Base title={post.data.title}>
	<Breadcrumb items={items} />
	<h1>{post.data.title}</h1>
</Base>
ホーム、お知らせ、休業日、記事タイトルの順に並んだパンくずリスト

「ホーム > お知らせ > 休業日 > 10月の定休日のお知らせ」の順に表示されました。3つ目の「休業日」がこの記事に付けたカテゴリーです。

手順3:構造化データ(BreadcrumbList)を出力する

検索エンジンにパンくずの階層を伝えるには、BreadcrumbListという構造化データをJSON-LDで出力します。EmDashのEmDashHeadが出力する構造化データは記事とサイトの情報で、パンくずは含まれません。手順1の部品を次のように書き換えます。

src/components/Breadcrumb.astro
---
interface Item {
	label: string;
	href?: string;
}

interface Props {
	items: Item[];
}

const { items } = Astro.props;

const jsonLd = {
	"@context": "https://schema.org",
	"@type": "BreadcrumbList",
	itemListElement: items.map((item, index) => ({
		"@type": "ListItem",
		position: index + 1,
		name: item.label,
		...(item.href ? { item: new URL(item.href, Astro.url).href } : {}),
	})),
};
---

<nav aria-label="パンくずリスト">
	<ol class="breadcrumb">
		{items.map((item) => (
			<li>
				{item.href ? <a href={item.href}>{item.label}</a> : <span aria-current="page">{item.label}</span>}
			</li>
		))}
	</ol>
</nav>

<script type="application/ld+json" set:html={JSON.stringify(jsonLd).replace(/</g, "\\u003c")} />
  • 表示に使うitemsから構造化データも作るので、表示と構造化データの内容がずれません
  • itemにはhttps://から始まるURLを入れます。new URL(item.href, Astro.url).hrefで、/postsのようなパスをURLに直しています
  • 最後の項目(今のページ)にはhrefがないので、itemを付けていません
  • .replace(/</g, "\\u003c")は、タイトルに</script>のような文字が入っていてもHTMLが壊れないようにする書き換えです

記事のページのHTMLに、次のJSON-LDが出力されます。

{
	"@context": "https://schema.org",
	"@type": "BreadcrumbList",
	"itemListElement": [
		{
			"@type": "ListItem",
			"position": 1,
			"name": "ホーム",
			"item": "https://example.com/"
		},
		{
			"@type": "ListItem",
			"position": 2,
			"name": "お知らせ",
			"item": "https://example.com/posts"
		},
		{
			"@type": "ListItem",
			"position": 3,
			"name": "休業日",
			"item": "https://example.com/category/holiday"
		},
		{
			"@type": "ListItem",
			"position": 4,
			"name": "10月の定休日のお知らせ"
		}
	]
}

Googleのリッチリザルトテストにこのコードを入れると、「パンくずリスト」が有効なアイテムとして検出されました。

書き方のポイント

カテゴリーがない記事にも対応する

...(category ? [ … ] : [])は、「カテゴリーがあるときだけ、その項目を足す」という書き方です。カテゴリーを付けていない記事では、その段が抜けます。

カテゴリーは複数付けられる

post.data.terms?.categoryには、付いているカテゴリーがすべて入っています。上の例では、[0]で最初の1つだけを使っています。

固定ページで使うとき

固定ページなら、「ホーム > ページのタイトル」の2段にします。渡す配列を変えるだけで、同じ部品を使い回せます。

WordPressとの違い

項目

WordPress

EmDash

出力する方法

プラグイン(Breadcrumb NavXTなど)や自作の関数

自作の共通パーツ

階層の決め方

投稿タイプやタクソノミーから自動で組み立てる

テンプレートごとに配列で渡す

HTMLの形

プラグインの設定で調整

パーツのファイルに直接書く

構造化データ

SEOプラグイン(Yoast SEOなど)が出力する

同じ部品の中でJSON-LDを出力する

次に読む記事

dynamic_sidebarにあたる書き方:EmDashのウィジェットエリア