Headless WordPress · Lesson 21 of 22 · 15 min

Featured images, WordPress HTML, and pages

Show featured images, style the HTML WordPress sends, and pull the About page from WordPress.

The loader already puts image on each post. Show it in PostCard:

src/components/PostCard.astro

---
import type { CollectionEntry } from 'astro:content';

interface Props {
  post: CollectionEntry<'blog'>;
}

const { post } = Astro.props;
const { title, description, pubDate, image } = post.data;
---

<article>
  {image && <img src={image.src} alt={image.alt} loading="lazy" />}
  <a href={`/blog/${post.id}`}>
    <h3>{title}</h3>
  </a>
  <time datetime={pubDate.toISOString()}>
    {pubDate.toLocaleDateString('en-US', { dateStyle: 'medium', timeZone: 'UTC' })}
  </time>
  <p>{description}</p>
</article>

Add to its <style>:

img {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: var(--radius);
  margin-bottom: 0.75rem;
}

aspect-ratio plus object-fit: cover keeps every card the same shape no matter what size the editor uploaded. loading="lazy" waits to download images until they scroll into view.

The alt text comes from the Alt Text field in the WordPress media library. Remind editors to fill it in.

2. Style the HTML from WordPress

The post body is HTML that WordPress wrote: <figure>, <img>, <blockquote>, block classes like wp-block-image. Your scoped styles can’t see it, because Astro only scopes the HTML inside your component.

That’s what :global() is for. In src/pages/blog/[id].astro, wrap the content in a div and style what’s inside it:

src/pages/blog/[id].astro

<!-- inside the article, replacing <Content /> -->
{image && <img class="cover" src={image.src} alt={image.alt} />}
<div class="content">
  <Content />
</div>
.cover {
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  margin: 1rem 0;
}

/* WordPress HTML isn't part of this component, so use :global() to reach it */
.content :global(img) {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
}

.content :global(figure) {
  margin: 2rem 0;
}

.content :global(figcaption) {
  color: var(--color-muted);
  font-size: 0.9rem;
  text-align: center;
}

.content :global(blockquote) {
  margin: 2rem 0;
  padding-left: 1.25rem;
  border-left: 3px solid var(--color-accent);
}

.content :global(iframe) {
  max-width: 100%;
}

.content :global(img) means “any img inside this component’s .content div”. The .content part stays scoped, so these rules can’t leak into the rest of the site.

Also add image to the destructured fields at the top: const { title, description, pubDate, tags, image } = post.data;

3. The About page from WordPress

Add a second collection for pages, using the same loader:

src/content.config.ts

// add below the blog collection
const pages = defineCollection({
  loader: wordpressLoader({ type: 'pages' }),
  schema: z.object({
    title: z.string(),
    description: z.string(),
  }),
});

export const collections = { blog, pages };

Delete src/pages/about.md and create about.astro in its place:

src/pages/about.astro

---
import BaseLayout from '../layouts/BaseLayout.astro';
import { getEntry, render } from 'astro:content';

const page = await getEntry('pages', 'about');

if (!page) {
  throw new Error('No WordPress page with the slug "about". Create one under Pages in WordPress.');
}

const { Content } = await render(page);
---

<BaseLayout title={page.data.title}>
  <article class="container prose">
    <h1>{page.data.title}</h1>
    <div class="content">
      <Content />
    </div>
  </article>
</BaseLayout>

<style>
  .prose {
    max-width: 680px;
    padding-top: 2rem;
  }

  .content :global(img) {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
  }
</style>

getEntry('pages', 'about') grabs one entry by ID. Since the loader uses the slug as the ID, that’s the WordPress page with the slug about.

The throw turns a missing page into a clear build error instead of a blank page. A loud error at build time beats a broken page in production.

Pages don’t have excerpts in WordPress, so description is an empty string. That’s fine. BaseLayout falls back to its default description.

You can delete MarkdownLayout.astro too. Nothing uses it now.