Tags, part 2. How to fake a hierarchy URL in Ghost.
How can we get hierarchies, including URLs like /programming/python/your-post-name ?
If you're just joining us, be sure to read part 1 of "Everything Cathy Knows About Tags."
This post had languished without content for a couple months, until I found myself fielding a question that I thought I'd already written an answer to. Nope, it had gotten lost in my drafts!
Can I have the category in the post URL?
Yes, this is easy and just requires one change to routes.yaml once, for any number of categories. Go back and read the post linked above, if all you want is something like /programming/your-post-name. The rest of this post is about more complicated (deeper) hierarchies.
How can we get hierarchy urls like programming/python/your-post-name in Ghost?
(In other words, how do we get subcategories in the URL?)
Getting more complex urls like /programming/python/your-post-name is not as easy. Here are a couple options:
- Make your primary tag actually be "Programming > Python" with the slug "programming-python", so your URLs are /{programming-python}/{your-post-name}. That's automatic and super easy to work with, no changes to routing needed beyond the first one to make the primary tag show up in the URLs. If you don't actually need to browse the hierarchy, this is a fast and easy option.
- Create a separate route for each category, filtered by that tag (i.e. Programming). Write a routes.yaml file that puts each category into the URL. Make the subcategory the primary tag, and the category later in the list of tags. That lets you get:
/programming/{primary_tag}/{your-post-name}
But if you also have recipes, you'll have to add an entry for that, too.
/recipes/{primary_tag}/{your-post-name}
... but at least it's only one routes.yaml entry per category, so hopefully not too many or too frequent changes. - Manually route every single post. That lets you do things like include every single tag, but it's going to be a pain in the butt, because you'll have to edit your routes file every single time you publish a post. I don't think this is worthwhile.
You can write a line in the routes file to serve up /programming/python/version-3.2/somethingelse/yourpostname, but you'll also need one for /programming/python/version-3.2/somethingelse/yourotherpost and /programming/python/version-3.2/somethingelse/yourthirdpost ... ugh! Yeah, I wouldn't do this. If you really want this in the URL, consider making your post's slug be python-version-3.2-somethingelse-your-actual-title. (You don't have to give your post this title! Just adjust the slug over on the right side menu after you set the title.)
Stay tuned for part 3, in which I share some hints for how to fake browsing your fake hierarchy. 🤯
Hey, before you go... If your finances allow you to keep this tea-drinking ghost and the freelancer behind her supplied with our hot beverage of choice, we'd both appreciate it!