タクソノミーは、コンテンツを整理するための分類システムです。EmDashには、カテゴリーとタグが組み込まれており、専門的な分類ニーズに対応するカスタムタクソノミーをサポートしています。
組み込みタクソノミー
EmDashは2つのデフォルトタクソノミーを提供しています:
| タクソノミー | タイプ | 説明 |
|---|---|---|
| カテゴリー | 階層型 | 親子関係を持つネストされた分類 |
| タグ | フラット型 | 階層なしのシンプルなラベル |
どちらもデフォルトで投稿コレクションで利用できます。
用語の管理
用語を作成する
Admin Dashboard
-
タクソノミーページにアクセスします(例:
/_emdash/admin/taxonomies/category) -
新規追加フォームに用語名を入力します
-
オプションで設定:
- スラッグ - URL識別子(名前から自動生成)
- 親 - 階層型タクソノミー用
- 説明 - 用語の説明
-
追加をクリック
Content Editor
-
エディターでコンテンツエントリーを開きます
-
サイドバーのタクソノミーパネルを探します
-
カテゴリーの場合、該当する用語のチェックボックスをオンにするか、+ 新規追加をクリックします
-
タグの場合、カンマで区切ってタグ名を入力します
-
コンテンツを保存します
API
以下のリクエストは、categoryタクソノミーに用語を作成します:
POST /_emdash/api/taxonomies/category/terms
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"slug": "tutorials",
"label": "Tutorials",
"parentId": "term_abc",
"description": "How-to guides and tutorials"
} 用語を編集する
-
タクソノミー用語ページにアクセスします
-
用語の横にある編集をクリックします
-
名前、スラッグ、親、または説明を更新します
-
保存をクリックします
用語を削除する
-
タクソノミー用語ページにアクセスします
-
用語の横にある削除をクリックします
-
削除を確認します
タクソノミーのクエリ
EmDashは、タクソノミー用語をクエリし、用語でコンテンツをフィルタリングする関数を提供します。
すべての用語を取得する
タクソノミーのすべての用語を取得:
import { getTaxonomyTerms } from "emdash";
// Get all categories (returns tree structure)
const categories = await getTaxonomyTerms("category");
// Get all tags (returns flat list)
const tags = await getTaxonomyTerms("tag");
階層型タクソノミーの場合、用語にはchildren配列が含まれます:
interface TaxonomyTerm {
id: string;
name: string; // Taxonomy name ("category")
slug: string; // Term slug ("news")
label: string; // Display label ("News")
parentId?: string;
description?: string;
children: TaxonomyTerm[];
count?: number; // Number of entries with this term
}
単一の用語を取得する
以下の例は、タクソノミーとスラッグで1つの用語を取得します:
import { getTerm } from "emdash";
const category = await getTerm("category", "news");
// Returns TaxonomyTerm or null
エントリーの用語を取得する
以下の例は、単一のエントリーに割り当てられたカテゴリーとタグを取得します:
import { getEntryTerms } from "emdash";
// Get all categories for a post
const categories = await getEntryTerms("posts", "post-123", "category");
// Get all tags for a post
const tags = await getEntryTerms("posts", "post-123", "tag");
用語でコンテンツをフィルタリングする
whereフィルターでgetEmDashCollectionを使用:
import { getEmDashCollection } from "emdash";
// Posts in the "news" category
const { entries: newsPosts } = await getEmDashCollection("posts", {
status: "published",
where: { category: "news" },
});
// Posts with the "javascript" tag
const { entries: jsPosts } = await getEmDashCollection("posts", {
status: "published",
where: { tag: "javascript" },
});
または便利な関数を使用:
import { getEntriesByTerm } from "emdash";
const newsPosts = await getEntriesByTerm("posts", "category", "news");
タクソノミーページの構築
カテゴリーアーカイブ
カテゴリー内の投稿をリストするページを作成:
---
import { getTaxonomyTerms, getTerm, getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";
export async function getStaticPaths() {
const categories = await getTaxonomyTerms("category");
// Flatten hierarchical tree for routing
function flatten(terms) {
return terms.flatMap((term) => [term, ...flatten(term.children)]);
}
return flatten(categories).map((cat) => ({
params: { slug: cat.slug },
props: { category: cat },
}));
}
const { category } = Astro.props;
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
where: { category: category.slug },
});
---
<Base title={category.label}>
<h1>{category.label}</h1>
{category.description && <p>{category.description}</p>}
<p>{category.count} posts</p>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
タグアーカイブ
タグ付きの投稿をリストするページを作成:
---
import { getTaxonomyTerms, getEmDashCollection } from "emdash";
import Base from "../../layouts/Base.astro";
export async function getStaticPaths() {
const tags = await getTaxonomyTerms("tag");
return tags.map((tag) => ({
params: { slug: tag.slug },
props: { tag },
}));
}
const { tag } = Astro.props;
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
where: { tag: tag.slug },
});
---
<Base title={`Posts tagged "${tag.label}"`}>
<h1>#{tag.label}</h1>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.data.slug}`}>{post.data.title}</a>
</li>
))}
</ul>
</Base>
カテゴリーリストウィジェット
投稿数付きのカテゴリーリストを表示:
---
import { getTaxonomyTerms } from "emdash";
const categories = await getTaxonomyTerms("category");
---
<nav class="category-list">
<h3>Categories</h3>
<ul>
{categories.map((cat) => (
<li>
<a href={`/category/${cat.slug}`}>
{cat.label} ({cat.count})
</a>
{cat.children.length > 0 && (
<ul>
{cat.children.map((child) => (
<li>
<a href={`/category/${child.slug}`}>
{child.label} ({child.count})
</a>
</li>
))}
</ul>
)}
</li>
))}
</ul>
</nav>
タグクラウド
使用状況に基づいたサイズでタグを表示:
---
import { getTaxonomyTerms } from "emdash";
const tags = await getTaxonomyTerms("tag");
// Calculate font sizes based on count
const counts = tags.map((t) => t.count ?? 0);
const maxCount = Math.max(...counts, 1);
const minSize = 0.8;
const maxSize = 2;
function getSize(count: number) {
const ratio = count / maxCount;
return minSize + ratio * (maxSize - minSize);
}
---
<div class="tag-cloud">
{tags.map((tag) => (
<a
href={`/tag/${tag.slug}`}
style={`font-size: ${getSize(tag.count ?? 0)}rem`}
>
{tag.label}
</a>
))}
</div>
コンテンツに用語を表示する
投稿にカテゴリーとタグを表示:
---
import { getEntryTerms } from "emdash";
interface Props {
collection: string;
entryId: string;
}
const { collection, entryId } = Astro.props;
const categories = await getEntryTerms(collection, entryId, "category");
const tags = await getEntryTerms(collection, entryId, "tag");
---
<div class="post-terms">
{categories.length > 0 && (
<div class="categories">
<span>Posted in:</span>
{categories.map((cat, i) => (
<>
{i > 0 && ", "}
<a href={`/category/${cat.slug}`}>{cat.label}</a>
</>
))}
</div>
)}
{tags.length > 0 && (
<div class="tags">
{tags.map((tag) => (
<a href={`/tag/${tag.slug}`} class="tag">
#{tag.label}
</a>
))}
</div>
)}
</div>
カスタムタクソノミー
専門的なニーズに応じて、カテゴリーとタグを超えたタクソノミーを作成します。
カスタムタクソノミーを作成する
管理APIを使用してタクソノミーを作成:
POST /_emdash/api/taxonomies
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"name": "genre",
"label": "Genres",
"labelSingular": "Genre",
"hierarchical": true,
"collections": ["books", "movies"]
}
カスタムタクソノミーを使用する
組み込みタクソノミーと同じ方法でカスタムタクソノミーをクエリして表示:
import { getTaxonomyTerms, getEmDashCollection } from "emdash";
// Get all genres
const genres = await getTaxonomyTerms("genre");
// Get books in a genre
const { entries: sciFiBooks } = await getEmDashCollection("books", {
where: { genre: "science-fiction" },
});
コレクションに割り当てる
タクソノミーは、適用されるコレクションを指定します:
{
"name": "difficulty",
"label": "Difficulty Levels",
"hierarchical": false,
"collections": ["recipes", "tutorials"]
}
タクソノミーAPIリファレンス
RESTエンドポイント
| エンドポイント | メソッド | 説明 |
|---|---|---|
/_emdash/api/taxonomies | GET | タクソノミー定義をリスト |
/_emdash/api/taxonomies | POST | タクソノミーを作成 |
/_emdash/api/taxonomies/:name/terms | GET | 用語をリスト |
/_emdash/api/taxonomies/:name/terms | POST | 用語を作成 |
/_emdash/api/taxonomies/:name/terms/:slug | GET | 用語を取得 |
/_emdash/api/taxonomies/:name/terms/:slug | PUT | 用語を更新 |
/_emdash/api/taxonomies/:name/terms/:slug | DELETE | 用語を削除 |
コンテンツに用語を割り当てる
以下のリクエストは、投稿にカテゴリー用語を割り当てます:
POST /_emdash/api/content/posts/post-123/terms/category
Content-Type: application/json
Authorization: Bearer YOUR_API_TOKEN
{
"termIds": ["term_news", "term_featured"]
}
次のステップ
- ブログを作成する - ブログでカテゴリーとタグを使用する
- コンテンツをクエリする - タクソノミー用語でフィルタリングする
- コンテンツを操作する - エディターで用語を割り当てる