Get primary_tag name when in tag.hbs


#1

I write a blog each time I travel in my campervan and use tags to separate the different trips into separate pages on my Ghost blog.

Ideally I would like the Home page to be a standard blog page, all the posts with the latest entry at the top. But for each of the completed trips I want a separate blog page based on the trip’s primary_tag (easy - I do this already) but with the posts in reverse order so the trip is a sequential story as opposed to blog. One way to achieve this seems to be to edit my tag.hbs file and ignore the tag list provided by the ghost engine and re-get all the tags in inverse order using:

{{!-- The main content area --}}
<main id="site-main" class="site-main outer" role="main">
    <div class="inner">

        {{#get "posts" filter="primary_tag:trip1" order="published_at asc" }}
        <div class="post-feed">
            <!-- `posts` from the parent context is overwritten by get -->
            {{#foreach posts}}
    
                    {{!-- The tag below includes the markup for each post - partials/post-card.hbs --}}
                    {{> "post-card"}}
     
            {{/foreach}}
        </div>
        {{/get}}        
    </div>
</main>

This works fine provided I hard code the primary_tag name in the filter. However I want to use this for several different pages with different primary_tags. An obvious solution would be to find the context of the primary_tag when in tag.hbs . I had hoped something simple like filter=“primary_tag:{{primary_tag}}” might work.

An alternative - I read somewhere that I can have tag-trip1.hbs, tag-trip2.hbs etc and the page http://myblog/tag/trip1 would use up the appropriate hbs file - this is not optimal as I would need multiple tag.hbs files differing by just the primary_tag filter, but would work. Only problem is, when I tried it the page seemed to use the generic tag.hbs not the specific tag-trip1.hbs

Any thoughts on how to achieve this without playing around inside the engine code?


#2

The {{get}} helper has a special behaviour on the filter parameter so you can use handlebars-like sub-expressions, eg:

{{#tag}}
    {{#get "posts" filter="primary_tag:{{slug}}" order="published_at asc" }}
        {{#foreach posts}}
            ...
        {{/foreach}}
    {{/get}}
{{/tag}}

In this case {{slug}} is present because you are in the Tag context.

We’re also working on a feature called Channels that will make it possible to customise post lists and also create different site sections such as http://mysite.com/trips/trip1


#3

Kevin - BRILLIANT! Thanks for such a prompt reply. I knew I had to be in the {{tag}} context but couldn’t figure out the appropriate expression. {{slug}} works perfectly!!

I notice that using {{get}} like this without a limit parameter then I get a default of 15 posts and when I scroll down I get the same 15 posts repeated rather than the next 15. No problem I can just use limit=../pagination.total in the {{get}} and get all the posts for the page in one fell swoop.

Site sections aren’t quite so important to me personally - I only have trips (and possibly a couple of meta-posts about the blog itself) and I have separate ghost blogs as separate subdomains for my other interests. If there is a less hacky way to control post ordering it would be nice - blogging trips has always been a problem - you want them in normal descending order when folk are reading them as the occur but in ascending storyline order when read post-event.

Thanks again and thanks for all the hard work in producing such a great piece of blogging software

Oh, and just to clear any confusion to others reading this. I’ve just discovered that having tag-trip1.hbs did in fact work correctly!!! I made a mistake in it which made me think it wasn’t working! Doh!


#4

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.