Gridsome conditional template generation

Viewed 211

I have blog posts categorised as 'jobs' within wordpress posts.

I want to create pages from posts that have category 'jobs'. I think this is a good use case for templates:

 templates: {
    WordPressPost: [
      {
        name: 'jobs',
        path: '/jobs/job-blog/:slug',
        component: './src/templates/Blogs.vue'
      }
    ],
  }

but this will create pages from all of the posts not just the one with category 'jobs'.

Is there a way to conditionally create pages from posts with a certain category?

I'm thinking something like:

 WordPressPost: [
      { 
        name: 'jobs',
        path: (node) => {
          if (node.category === 'jobs') {
            return `/jobs/job-blog/${node.slug}`
          }
        },
        component: './src/templates/Blogs.vue'
      }
    ]

This fails Error: Duplicate key for property path: /

Seems like it's generating other pages at root?

Many thanks!

0 Answers
Related