To add a blog to an Astro theme, define a blog content collection in src/content.config.ts, put Markdown posts in src/content/blog/, and create two pages: src/pages/blog/index.astro to list posts and src/pages/blog/[...slug].astro to render each one inside the theme’s existing layout. The new pages inherit the theme’s header, fonts and colours automatically.
Key Takeaways
- A blog is one content collection plus two pages; no plugin is needed.
- Use the
glob()loader and a Zod schema so bad frontmatter fails the build.- Wrap posts in the theme’s own base layout so they match the rest of the site.
- Filter out drafts and sort by date in one helper you reuse.
- Add the blog to the navigation and, optionally, an RSS feed.
Define the collection
If the theme already has src/content.config.ts, add a collection to it. Otherwise create the file:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const blog = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/blog' }),
schema: ({ image }) =>
z.object({
title: z.string(),
description: z.string(),
pubDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
cover: image().optional(),
draft: z.boolean().default(false),
}),
});
export const collections = { blog };
If the theme’s config already exports collections, add blog to the existing object instead of exporting a second one. The content collections docs explain loaders and schemas in more depth.
Write a first post
---
title: "Welcome to the new blog"
description: "Why we started writing, and what to expect."
pubDate: 2026-11-02
---
This is the first post. Markdown, images and code blocks all work here.
Save it as src/content/blog/welcome.md. The file name becomes the post’s id and its URL: /blog/welcome/. If you want MDX, run npx astro add mdx first.
Create the post page
Find the layout the theme’s other pages use (look at the top of src/pages/about.astro or similar) and reuse it:
---
// src/pages/blog/[...slug].astro
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
export async function getStaticPaths() {
const posts = await getCollection('blog', ({ data }) => !data.draft);
return posts.map((post) => ({ params: { slug: post.id }, props: { post } }));
}
const { post } = Astro.props;
const { Content } = await render(post);
---
<BaseLayout title={post.data.title} description={post.data.description}>
<article class="prose mx-auto max-w-3xl px-4 py-16">
<h1>{post.data.title}</h1>
<time datetime={post.data.pubDate.toISOString()}>
{post.data.pubDate.toLocaleDateString('en-US', { dateStyle: 'long' })}
</time>
<Content />
</article>
</BaseLayout>
Match the layout’s real prop names; some themes call them title and description, others pass a single meta object. The prose class comes from the Tailwind typography plugin. If the theme does not include it, either install @tailwindcss/typography or style headings, lists and code in a scoped <style> block.
Create the blog index
---
// src/pages/blog/index.astro
import { getCollection } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
---
<BaseLayout title="Blog" description="Notes, updates and guides.">
<section class="mx-auto max-w-3xl px-4 py-16">
<h1>Blog</h1>
<ul>
{posts.map((post) => (
<li>
<a href={`/blog/${post.id}/`}>{post.data.title}</a>
<p>{post.data.description}</p>
</li>
))}
</ul>
</section>
</BaseLayout>
If the theme has a card component (for projects or case studies), reuse it for the post list so the blog looks native. Once you pass 20 or so posts, add pagination.
Add it to the menu and feed
Add { label: 'Blog', href: '/blog/' } to the theme’s navigation array; editing the navigation menu shows where that usually lives. For an RSS feed, install @astrojs/rss and follow our RSS docs.
Then build:
# Terminal
npm run build
A frontmatter field that breaks the schema stops the build with the file name and field, which is the point of defining one. For a fuller walkthrough with tags, reading time and related posts, see build a blog with Astro. If you would rather start with the blog already built, the Blog & Editorial themes ship one.
FAQ
Can I add a blog to any Astro theme?
Yes. Any Astro theme is a normal Astro project, so you can add a content collection and two pages to it. The only theme-specific work is reusing its layout and styles.
Do I need a CMS to add a blog to Astro?
No. Markdown files in src/content/blog/ are enough, and they live in Git with the rest of the site. You can add a Git-based or headless CMS later without changing the pages.
Why is my Astro blog post page empty?
Usually render() was not called or Content was not placed in the template. Check that the page calls await render(post) and renders <Content />.
How do I hide draft posts in Astro?
Add a draft boolean to the schema and filter it out in getCollection('blog', ({ data }) => !data.draft) on both the index and the post page.