date.phpにあたるページ:EmDashで月別アーカイブを作る
WordPressのdate.phpにあたる、月別アーカイブをEmDashで作る方法です。月の一覧と月ごとのページを作り、記事がない月や不正なURLが404になることも確かめました。
WordPressには、/2026/10/のような月別アーカイブが最初からあり、date.phpで見た目を作りました。EmDashには月別のページは用意されていません。記事の一覧を取り、公開日の年月でまとめる形で作りました。
先に結論
- 月の一覧は、記事を取って、公開日の年月ごとに件数を数えて作ります
- 月ごとのページは、
src/pages/archive/[month].astroを置き、年月が合う記事だけを残します - 記事が100件を超えるサイトでは、この書き方のままでは古い記事が出なくなります
手順1:月の一覧を作る
wp_get_archives()にあたる部分です。
---
import { getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";
const { entries: posts } = await getEmDashCollection("posts", {
orderBy: { published_at: "desc" },
limit: 100,
});
// 「2026-11」のような年月ごとに、件数を数える
const counts = new Map<string, number>();
for (const post of posts) {
const date = post.data.publishedAt;
if (!date) continue;
const month = date.toLocaleDateString("sv-SE", { timeZone: "Asia/Tokyo" }).slice(0, 7);
counts.set(month, (counts.get(month) ?? 0) + 1);
}
const months = [...counts.entries()];
---
<Base title="月別アーカイブ">
<h1>月別アーカイブ</h1>
<ul class="months">
{months.map(([month, count]) => (
<li>
<a href={`/archive/${month}`}>{month.replace("-", "年")}月({count})</a>
</li>
))}
</ul>
</Base>
記事7件が、4つの月に分かれて表示されました。
年月を取り出している行の意味
date.toLocaleDateString("sv-SE", { timeZone: "Asia/Tokyo" })は日付を2026-11-03の形の文字にします。sv-SE(スウェーデン)を指定しているのはこの国の日付の書き方が「年-月-日」だからです。そこから.slice(0, 7)で先頭の7文字を取ると、2026-11になります。
timeZone: "Asia/Tokyo"は、サーバーの設定に関係なく日本時間で月を決めるための指定です。付けないと、月の初めの朝に公開した記事が前の月に入ることがあります。くわしくは、get_the_dateにあたる書き方:EmDashで日付の表示形式を変えるで説明しています。
手順2:月ごとのページを作る
---
import { getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";
const month = Astro.params.month ?? "";
if (!/^\d{4}-\d{2}$/.test(month)) return Astro.rewrite("/404");
const { entries: all } = await getEmDashCollection("posts", {
orderBy: { published_at: "desc" },
limit: 100,
});
const posts = all.filter((post) => {
const date = post.data.publishedAt;
return date?.toLocaleDateString("sv-SE", { timeZone: "Asia/Tokyo" }).startsWith(month);
});
if (posts.length === 0) return Astro.rewrite("/404");
const heading = `${month.replace("-", "年")}月のお知らせ`;
---
<Base title={heading}>
<h1>{heading}</h1>
<ul class="month-posts">
{posts.map((post) => (
<li>
<time datetime={post.data.publishedAt?.toISOString()}>
{post.data.publishedAt?.toLocaleDateString("ja-JP", { timeZone: "Asia/Tokyo" })}
</time>{" "}
<a href={`/posts/${post.id}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
開いたURL | 結果 |
|---|---|
| 10月の記事2件が表示された |
| 404 |
| 404 |
/^\d{4}-\d{2}$/.test(month):URLが「数字4桁-数字2桁」の形かどうかを確かめています.filter(…):取った記事の中から、公開日の年月が合うものだけを残しています{" "}:日付とタイトルの間に空白を1つ入れています。書かないと、2つがつながって表示されます
記事が多いサイトでの注意
この書き方は新しい順に100件を取り、その中で月ごとに分けています。101件目より古い記事は、月の一覧にも月ごとのページにも出ません。
月ごとのページは、公開日の範囲で絞る形に変えると、件数に関係なくその月の記事が取れます。whereにpublished_atの範囲を書きます。
const [year, monthNumber] = month.split("-").map(Number);
// 日本時間の月初め(UTCでは前の日の15時)
const from = new Date(Date.UTC(year, monthNumber - 1, 1, -9));
const to = new Date(Date.UTC(year, monthNumber, 1, -9));
const { entries: posts } = await getEmDashCollection("posts", {
where: { published_at: { gte: from.toISOString(), lt: to.toISOString() } },
orderBy: { published_at: "desc" },
});gteは「その日時以降」、ltは「その日時より前」という意味です/archive/2026-10で10月の2件、/archive/2026-11で11月の2件が表示されました- 記事がない月は、
postsが空になるので、これまでと同じく404になりました
WordPressとの対応
項目 | WordPress | EmDash |
|---|---|---|
月別のページ | 最初からある | 自分で作る |
テンプレート |
|
|
月の一覧 |
| 記事を取って、年月ごとに数える |
URL |
|
|
月の判定 |
|
|
リダイレクトについては、EmDashでリダイレクトを設定する2つの方法で説明しています。
ウィジェットにも月別の部品がある
ウィジェットの決まった部品の中に、月別の一覧(core:archives)があります。ウィジェットエリアに足すと、「November 2026 (2)」のように、月の名前と件数が英語で並びました。
リンク先は、/archives/2026/11の形です。Starterテンプレートにはこのページがないので、そのままでは404になります。src/pages/archives/[year]/[month].astroを作ると、そのURLで表示されました。