How can I display all posts tagged with a specific tag on a different page to the homepage

Hi,

I am not sure if I am asking this question correctly, as I am still learning to use Ghost. I want to have a page (separate from the homepage) that displays all posts I have tagged with “terms”.

I then want to permanently hide any post tagged with “terms” from the homepage.

How can I do this?

Thanks

Mel

1 Like

There are a couple of ways to achieve this. I’ll start by suggesting the easiest method, which is to use a custom routes.yaml file. Ghost’s documentation is pretty good on this.

What you’ll want to do is go to Settings > Labs > Download current routes.yaml file.

Open that file. Under collections, you’ll find Ghost’s default collection:

  /:
    permalink: /{slug}/
    template: index

You’ll want to modify that by adding another collection like this:

  /:
    permalink: /{slug/
    template: index
    filter: tag:-terms
  /terms/:
    permalink: /terms/{slug}/
    template: index
    filter: tag:terms

(What’s happening here is that we’re filtering two collections. The first one excludes posts tagged with terms (-), and the second includes only posts tagged with terms. We’re also specifying where we want these collections to load and which template to use.

Save the file and upload it to Settings > Labs > Routes.

If everything went OK :crossed_fingers: all your posts tagged with “terms” will be hidden from the homepage and collected at yoursite.com/terms!

Two things to know:

  1. YAML files are whitespace sensitive. If you get errors on upload, be sure to check your file for formatting.
  2. This won’t present all your terms posts at /terms/, but rather the limit in your theme’s config. It will automatically create a paginated route for additional posts. It’s possible to create a page that holds all terms posts, but it’s a bit more involved.

Let me know how it goes.

2 Likes

Hi,

Thank you for your reply and your assistance. I have gotten the routing via routes.yaml working fine. I made a test post, tagged it with the tag and posed the post was hidden from the homepage but displayed in the corresponding tag archive.

I would have liked to have a page that displayed the posts in a way similar to the homepage, but I can use the tag archive for this. The main issue was that I wanted to collect all my posts for Terms and Conditions, Privacy Policies, Terms of Service, and links to the respective policies for stripe etc., into one place.

That was why I thought of having a page; however, the tag archive will work fine for this purpose, and I can link to that.

I do have a follow-up question, however. Is it possible to direct a URL such as www.mjwolfe.me/terms to the tag archives URL www.mjwolfe.me/tag/terms?

I think users looking for the various policies might not realise that they have to type /tag/terms - only typing /terms instead, which would not get them to where they wanted to go.

Although if that would be too complicated or is not possible, I understand, and I have linked to the tag archive in the footer anyway.

Thank you for all the help as I learn to build my site with Ghost CMS- it is greatly appreciated.

Mel

Glad you got it working!

With the custom routing, all your posts tagged with “terms” should show up on /terms, so I don’t think you should need the redirect–unless I’m missing something. If you do want to implement a redirect, it is possible: Implementing redirects in Ghost

Reading your response, though, I want to make sure we’re not over engineering this.

How many posts will you have tagged “terms”? If it’s not too many, you could go another route:

  • Create a page called “Terms”
  • Paste links to the different relevant posts. Ghost will create bookmarks for each post, so it will be like a collection

Hi,

Thanks again for your response. I am not sure I explained myself correctly earlier, as I am still learning to use the system.

I changed the tag slug from “terms” to “tc.”

So, this is what I have currently:

  • I have a tag named Terms and Policies, with the slug tc
  • I have made a test post and tagged it with the tag Terms and Policies
  • The test post can be found in the tag archive www.mjwolfe.me/tag/tc
  • The test post does not display on the homepage, which is the correct behaviour

Thanks to your earlier help setting up custom routing, everything works as expected. There was, however, one more step that I wanted to take if it would be possible to do and not too complicated so that I could do it.

When somebody types in the URL - mjwolfe.me/terms I would like them to get automatically redirected to the tag archive found at mjwolfe.me/tag/tc.

The tc tag archive would house no more than ten posts. Those posts would rarely if ever, change.

Sorry for the earlier confusion. I am still learning the system, so I think I might not have described what I wanted to do very well before.

Thanks again,

Mel

Hi Mel,

Redirects and custom routing are the harder parts of Ghost, you seem to be handling them pretty effortlessly :smile:

Redirecting from /tag/tc to /terms is straightforward.

You can upload a redirects.yaml file to Settings > Labs > Upload redirects. The contents of the file should be as follows:

301:
  /tag/tc/: /terms/

Like last time, ensure that the white space is right. Once you upload this file and save, visitors to tag/tc will be automatically redirected to /terms/.

Let me know how it goes!

1 Like

Hi,

Thanks again for your help. I got that working too now as well. However, after reading your responses, I took what you had taught me, combined it with what I had learned from the links you provided me, and came up with this.

I have now created an internal tag that does not need to be visible to the public but that I can use to filter posts and hide them from the homepage.

I then created a collection for the Terms and Conditions with a corresponding public tag as shown below.

routes:

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-hash-hidden
  /Terms and Conditions/:**
    permalink: /tnc/{slug}/**
    template: index**
    filter: tag:tnc**

......

After uploading the above routes.yaml file, I set about creating a redirect that will allow my users to easily find all the posts that I have tagged with Terms and Conditions.

Here is what I have

redirects:

301:
  /tos: /tag/tnc

302:

So, I can now do the following:

  • Filter the homepage for any post tagged with #hidden
  • Prevent any post tagged with #hidden from being displayed on the homepage
  • Provide my users with an easy to remember URL mjwolfe.me/tos where they can find all posts that I have tagged as being Terms and Conditions.

I now plan to extend this out - using internal tags to create other collections for newsletters, a blog, my published works, etc., allowing me to categorise each post into a collection and then use redirects to provide an easy-to-remember URL for those collections.

I am not sure if this was what I was meant to do with the routes.yaml, and redirects.yaml files, but hopefully, it won’t break anything.

Thanks

Mel

Great! It’s seems like you’re really cooking now.

If you get stuck, don’t hesitate to reach out here in the Forum. There’s lots of us who are happy to help :smile:

Is it possible to create a collection within a collection.

For example:
collection 1 = Travel Guides
collection 1.1 = Countries

So that when someone goes to the travel guides page, they can see a list of countries we have travel guides for.

Thanks