Skip to content

Commit 5d59364

Browse files
committed
Generate custom WordPress collections
1 parent a50d6bf commit 5d59364

13 files changed

Lines changed: 260 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ builds. The repository contains no WordPress or Spacefast credentials.
4444
- `/blog/` and `/blog/[slug]/` — all published posts
4545
- `/category/[slug]/` — published categories
4646
- `/[slug]/` — published WordPress pages
47-
47+
- `/case-studies/[slug]/` — the custom `case_study` REST collection
48+
- `/resources/[slug]/` — the custom `resource` REST collection
49+
- `/launch-notes/[slug]/` — the custom `launch_note` REST collection
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
import SiteLayout from "../layouts/SiteLayout.astro";
3+
import PostCard from "./PostCard.astro";
4+
import type { DemoPage, DemoPost } from "../lib/wordpress";
5+
6+
interface Props {
7+
description: string;
8+
eyebrow: string;
9+
items: DemoPost[];
10+
pages: DemoPage[];
11+
path: string;
12+
title: string;
13+
}
14+
15+
const { description, eyebrow, items, pages, path, title } = Astro.props;
16+
---
17+
18+
<SiteLayout title={title} description={description} pages={pages}>
19+
<header class="page-header">
20+
<p class="eyebrow">{eyebrow}</p>
21+
<h1>{title}</h1>
22+
<p>{description}</p>
23+
</header>
24+
<section class="post-grid section">
25+
{items.map((item) => <PostCard post={item} hrefPrefix={path} />)}
26+
</section>
27+
</SiteLayout>
28+

‎src/components/ContentDetail.astro‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
import SiteLayout from "../layouts/SiteLayout.astro";
3+
import {
4+
contentOf,
5+
excerptOf,
6+
featuredImage,
7+
formattedDate,
8+
titleOf,
9+
type DemoPage,
10+
type DemoPost,
11+
} from "../lib/wordpress";
12+
13+
interface Props {
14+
backLabel: string;
15+
backPath: string;
16+
eyebrow: string;
17+
item: DemoPost;
18+
pages: DemoPage[];
19+
}
20+
21+
const { backLabel, backPath, eyebrow, item, pages } = Astro.props;
22+
const image = featuredImage(item);
23+
---
24+
25+
<SiteLayout title={titleOf(item)} description={excerptOf(item)} pages={pages}>
26+
<article class="article">
27+
<header>
28+
<p class="eyebrow">{eyebrow} · {formattedDate(item.date)}</p>
29+
<h1>{titleOf(item)}</h1>
30+
</header>
31+
{image?.source_url && (
32+
<figure class="article-hero">
33+
<img src={image.source_url} alt={image.alt_text ?? ""} />
34+
</figure>
35+
)}
36+
<div class="wp-content" set:html={contentOf(item)} />
37+
<a class="button secondary" href={backPath}>← {backLabel}</a>
38+
</article>
39+
</SiteLayout>
40+

‎src/components/PostCard.astro‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ import {
1010
interface Props {
1111
post: DemoPost;
1212
featured?: boolean;
13+
hrefPrefix?: string;
1314
}
1415
15-
const { post, featured = false } = Astro.props;
16+
const { post, featured = false, hrefPrefix = "/blog" } = Astro.props;
1617
const image = featuredImage(post);
18+
const href = `${hrefPrefix}/${post.slug}/`;
1719
---
1820

1921
<article class:list={["post-card", { featured }]}>
2022
{image?.source_url && (
21-
<a class="post-card-image" href={`/blog/${post.slug}/`} tabindex="-1">
23+
<a class="post-card-image" href={href} tabindex="-1">
2224
<img src={image.source_url} alt={image.alt_text ?? ""} loading="lazy" />
2325
</a>
2426
)}
2527
<div class="post-card-copy">
2628
<p class="eyebrow">{formattedDate(post.date)}</p>
27-
<h2><a href={`/blog/${post.slug}/`}>{titleOf(post)}</a></h2>
29+
<h2><a href={href}>{titleOf(post)}</a></h2>
2830
{excerptOf(post) && <p>{excerptOf(post)}</p>}
29-
<a class="text-link" href={`/blog/${post.slug}/`}>Read story <span>→</span></a>
31+
<a class="text-link" href={href}>Read more <span>→</span></a>
3032
</div>
3133
</article>
32-

‎src/layouts/SiteLayout.astro‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const pageTitle = title === "Field Notes" ? title : `${title} · Field Notes`;
3434
</a>
3535
<nav aria-label="Main navigation">
3636
<a href="/blog/">Stories</a>
37-
{pages.filter((page) => page.slug !== "home").slice(0, 4).map((page) => (
37+
<a href="/case-studies/">Case studies</a>
38+
<a href="/resources/">Resources</a>
39+
{pages.filter((page) => ["how-it-works", "about"].includes(page.slug)).map((page) => (
3840
<a href={`/${page.slug}/`}>{titleOf(page)}</a>
3941
))}
4042
</nav>
@@ -51,4 +53,3 @@ const pageTitle = title === "Field Notes" ? title : `${title} · Field Notes`;
5153
</footer>
5254
</body>
5355
</html>
54-

‎src/lib/wordpress.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async function listAll<T>(
6666
let postsPromise: Promise<DemoPost[]> | undefined;
6767
let pagesPromise: Promise<DemoPage[]> | undefined;
6868
let categoriesPromise: Promise<WordPressTerm[]> | undefined;
69+
const customCollections = new Map<string, Promise<DemoPost[]>>();
6970

7071
export function getPosts(): Promise<DemoPost[]> {
7172
postsPromise ??= listAll<DemoPost>(wp.posts, {
@@ -89,6 +90,29 @@ export function getCategories(): Promise<WordPressTerm[]> {
8990
return categoriesPromise;
9091
}
9192

93+
function getCustomCollection(restBase: string): Promise<DemoPost[]> {
94+
const existing = customCollections.get(restBase);
95+
if (existing) return existing;
96+
const pending = listAll<DemoPost>(wp.collection<DemoPost>(restBase), {
97+
_embed: true,
98+
orderBy: "date",
99+
});
100+
customCollections.set(restBase, pending);
101+
return pending;
102+
}
103+
104+
export function getCaseStudies(): Promise<DemoPost[]> {
105+
return getCustomCollection("case_study");
106+
}
107+
108+
export function getResources(): Promise<DemoPost[]> {
109+
return getCustomCollection("resource");
110+
}
111+
112+
export function getLaunchNotes(): Promise<DemoPost[]> {
113+
return getCustomCollection("launch_note");
114+
}
115+
92116
export function titleOf(item: WordPressPost | WordPressPage): string {
93117
return plainText(item.title?.rendered) || "Untitled";
94118
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
import type { GetStaticPaths } from "astro";
3+
import ContentDetail from "../../components/ContentDetail.astro";
4+
import {
5+
getCaseStudies,
6+
getPages,
7+
type DemoPost,
8+
} from "../../lib/wordpress";
9+
10+
export const getStaticPaths = (async () => {
11+
const items = await getCaseStudies();
12+
return items.map((item) => ({
13+
params: { slug: item.slug },
14+
props: { item },
15+
}));
16+
}) satisfies GetStaticPaths;
17+
18+
interface Props {
19+
item: DemoPost;
20+
}
21+
22+
const { item } = Astro.props;
23+
const pages = await getPages();
24+
---
25+
26+
<ContentDetail
27+
item={item}
28+
pages={pages}
29+
eyebrow="Case study"
30+
backLabel="All case studies"
31+
backPath="/case-studies/"
32+
/>
33+

‎src/pages/case-studies/index.astro‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
import ContentCollection from "../../components/ContentCollection.astro";
3+
import { getCaseStudies, getPages } from "../../lib/wordpress";
4+
5+
const [items, pages] = await Promise.all([getCaseStudies(), getPages()]);
6+
---
7+
8+
<ContentCollection
9+
title="Case studies"
10+
eyebrow="Fictional teams · Real workflow"
11+
description="How publishing teams can keep WordPress while shipping a fast, static public site."
12+
items={items}
13+
pages={pages}
14+
path="/case-studies"
15+
/>
16+

‎src/pages/index.astro‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import PostCard from "../components/PostCard.astro";
33
import SiteLayout from "../layouts/SiteLayout.astro";
44
import {
55
getCategories,
6+
getCaseStudies,
67
getPages,
78
getPosts,
89
} from "../lib/wordpress";
910
10-
const [posts, pages, categories] = await Promise.all([
11+
const [posts, pages, categories, caseStudies] = await Promise.all([
1112
getPosts(),
1213
getPages(),
1314
getCategories(),
15+
getCaseStudies(),
1416
]);
1517
const [lead, ...latest] = posts;
1618
---
@@ -55,6 +57,20 @@ const [lead, ...latest] = posts;
5557
</section>
5658
)}
5759

60+
{caseStudies.length > 0 && (
61+
<section class="section">
62+
<div class="section-heading">
63+
<h2>Built for real publishing teams</h2>
64+
<a class="text-link" href="/case-studies/">All case studies <span>→</span></a>
65+
</div>
66+
<div class="post-grid">
67+
{caseStudies.map((item) => (
68+
<PostCard post={item} hrefPrefix="/case-studies" />
69+
))}
70+
</div>
71+
</section>
72+
)}
73+
5874
{categories.length > 0 && (
5975
<section class="category-strip">
6076
<p class="eyebrow">Browse by trail</p>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
import type { GetStaticPaths } from "astro";
3+
import ContentDetail from "../../components/ContentDetail.astro";
4+
import { getLaunchNotes, getPages, type DemoPost } from "../../lib/wordpress";
5+
6+
export const getStaticPaths = (async () => {
7+
const items = await getLaunchNotes();
8+
return items.map((item) => ({
9+
params: { slug: item.slug },
10+
props: { item },
11+
}));
12+
}) satisfies GetStaticPaths;
13+
14+
interface Props {
15+
item: DemoPost;
16+
}
17+
18+
const { item } = Astro.props;
19+
const pages = await getPages();
20+
---
21+
22+
<ContentDetail
23+
item={item}
24+
pages={pages}
25+
eyebrow="Launch note"
26+
backLabel="All launch notes"
27+
backPath="/launch-notes/"
28+
/>
29+

0 commit comments

Comments
 (0)