Using routes.yaml to change sort order of tag pages (in journal theme)

Hi - I’m using the theme ‘journal 1.0.0’ with a Start Plan.

Through the Labs tab in the Ghost admin, I am able to change the sort order of posts to oldest-first on the home/index page by changing the routes.yaml file to:

 collections:
  /:
    permalink: /{slug}/
    template: index
    order: published_at asc

However, what I actually want is for the ‘/tags/{slug}’ pages to sort in ascending order.

In other words, when someone clicks on a tag under “Topics”, I want them to go to an archive page for that tag and for the posts to be sorted from oldest to newest.

I’ve tried tweaking collections, channels and routes all to no avail. So, does anyone know how I would do this via the routes.yaml file?

Thanks!

Could you share of the things you tried didn’t work?

I believe you’ve got the right idea to use routes.yml to add an entry with an order key like the one above.

Thanks for your reply!

I’ve tried this as an experiment:

collections:
  /titles/:  
    permalink: /titles/{slug}/
    template: tags || posts // i.e. tried both as templates
    order: published_at asc

And this (which is the route I want):

collections:
  /tag/:  
    permalink: /tag/{slug}/
    template: tag
    order: published_at asc

And I tried using the same as above in a channel (titles & tags as the route) too, e.g.:

routes:
  /tag/:
    controller: channel
    filter: tag:[title]
    order: published_at asc

When things work, the listed posts on the /tags/my-tag still always display in descending order.

As I say, all I really want is for all the /tags/my-tag pages to display in ascending order. And for the home page to continue to display in descending order.

Is editing the tag.hbs file an option?

From reading the docs on custom routing, I only see how to change the sort order for specific tags or routes and not how to change the sort order for all tags at once. If that’s supported, the tags could be clearer.

For changing the sort order on a single index page, there’s a tutorial here:

If you don’t have a lot of tags, you could repeat that approach for your other tags.

Otherwise, it will take some input from a Ghost dev or a deeper dive into the Ghost code to figure how how to chang ethe default post sort order.

Thanks - unfortunately not on the $11/mo Starter plan as far as I know. Can *.hbs files contain logic like sort order?

Thanks! I’d seen that article and yes, it works on an individual tag basis.

For example:

routes:

   /tag/my-tag/:
      controller: channel
      order: published_at asc

If there’s no other option, I guess I’ll have to manually modify this each time I create a new tag page that I want to sort ascending.

Can *.hbs files contain logic like sort order?

Absolutely. Lots of filtering and sorting and how many do you want options in the .hbs file. If you’re interested, take a look at the #get helper.

But yeah, it’s going to have to be a routes.yaml solution on the Starter plan. :slight_smile: (Well, or code injection, but I really don’t think you want to resort the whole page with javascript!)

Thanks Cathy!

Sticking to the Starter plan for now—it’s already quite a financial step up from self-hosting WordPress sites.

And given that one of appeals of Ghost over WP is simplicity, you’re right on the resorting with JavaScript!

routes.yaml is is… for now.

BTW, @Cathy_Sarisky & @markstos - is there any way of getting a list of the templates used in the “journal 1.0.0” theme so that I can target them with the routes.yaml file? Sorting is working but now I’m getting a different template on the ascending tag page in spite of using template:tag

Thanks!

I think I have a solution for you:

In the Ghost admin area, go to “Design > Change Theme > Advanced > Journal > … Download”.

So you’ll have just downloaded your theme file as a “zip” file. Unpack the Zip file so you can see and edit the files within.

Find the file named “index.hbs” and open it.

In the file, you’ll find a block like this:

{{#get "posts" include="authors" limit="all"}}

The syntax of this “get helper” is documented here:

Notably, one of the things yoiu can add is:

order="published_at asc"

So add that, save, re-zip and re-upload.

Now all your index pages should be sorted oldest to newest by default!

Now, if the Journal theme does not have a separate “home.hbs”, you’ve got more work to do if you want the home page sorted newest to oldest instead.

In that case, copy index.hbs to home.hbs and remove the edit you just made.

According to other docs ( How to build a custom homepage for your Ghost theme ), home.hbs is automatically recognized as the homepage template if it exists, so no further custom routing is needed.

We were approaching this as a routing problem, but it seems it’s easier to solve with a theme update!

1 Like

The OP has a starter plan, so editing the theme isn’t an option.

Thank you both for the help—I really appreciate your time and expertise on helping me to find a solution and learn something.

There’s a Github repo for the Journal theme here:

From that, you can confirm that the template you want to target is “tag.hbs”-- that’s the template used to display all the posts with the same tag.

1 Like