#Generate category pages

4 messages · Page 1 of 1 (latest)

woven bison
#

I have a category in my frontmatter like this
category: Slovenia 2022

How do I generate all category pages automatically? And list out all posts with that category on those pages.

It would also be nice, but not necessary, if I could have a custom slug and description for each category.

hearty carbon
#

I'd check out this section in the tutorial. It's for generating tags, so similar to what you're after!
https://docs.astro.build/en/tutorial/5-astro-api/2/

Any specific questions, feel free to ask away. I went through this and found it worked well

Astro Documentation

Tutorial: Build your first Astro blog —
Use getStaticPaths() to create multiple pages (routes) at once

woven bison
#

That page does not use content collections.

frozen wing
#

here's my tag page (that uses content collections)

---
import { getCollection } from 'astro:content';

export async function getStaticPaths() {
  const allPosts = await getCollection('blog').then((collection) => collection.reverse());

  const tags: string[] = [];

  allPosts.forEach((post) => {
    post.data.tags.forEach((tag) => {
      tags.push(tag.toLowerCase());
    });
  });

  return Array.from(new Set(tags)).map((tag) => {
    return {
      params: { tag },
      props: {
        tag,
        blogposts: allPosts.filter((post) => post.data.tags.map(tag => tag.toLowerCase()).includes(tag)),
      },
    };
  });
}

const { tag, blogposts } = Astro.props;
---

This might give you some ideas