How to include tags in Ghost post URLs
For a new Ghost blog, I wanted to organise my posts by using their primary tag in each posts's URL. For this site, the homepage is not a normal list of recent posts, and I instead wanted to use tags as major categories or "folders" of content.
This is the URL structure that I wanted to achieve (the domain is fictional 😊):
Homepagecookwell.com/
(an intro to the site, some featured posts)
Tag pagecookwell.com/cooking/
(blog-type list of posts for each category)
Post pagecookwell.com/cooking/30-ways-to-cook-baked-beans/
It's easy to get this to work; you just have to edit one file. Just bear in mind that you can only do this for primary tags (the first tag listed on a post), as you can only assign each post to one URL "folder".
How to add tags to your post URLs
Download your current routes.yaml
file. You can do this from the Labs page in your Ghost admin.
Edit the collections and taxonomies sections as follows, then re-upload the file.
- The collection at
/
should have itspermalink
changed from/{slug}/
to/{primary_tag}/{slug}/
- The
tag
value in thetaxonomies
section should be changed from/tag/{slug}/
to just/{slug}/
Here's the changed file:
routes:
collections:
/:
permalink: /{primary_tag}/{slug}/
template: index
taxonomies:
tag: /{slug}/
author: /author/{slug}/
First, we want to add the tag into each post's URL, so we just append the primary_tag
value to the permalink
value in the root collection.
Secondly, we need to change the tag page URL. By default, Ghost puts every tag list view at /tag/tag-name/
so we just remove the /tag
prefix and change it to render just /tag-name/
.
There are many things you can add into URLs in Ghost, like dates, for example. For a full list of properties that can be in a URL, go to ghost.org/docs/api/v3/handlebars-themes/routing/properties/