Accessing internal post tags for content creation

I am trying to get my instance internatinalized and I am giong crazy a little bit.
Trying the whole day to solve a simple problem and read a lot, but can’t seem to find a solution.
I am using the Edition theme and am in the related-posts.hbs

I have multiple posts. Each tagged with a language hashtag (#en, #de, #fr, …)
{{#has tag=“#en”}} works in the {{post}} context.

What I try to achieve are two things:

  1. give the text “You might also like…” in the specific language
  2. filter the related posts by the given internal language tag additionally, so only Posts in the specific current post language are shown.

The original code is:

{{#get "posts" limit="5" filter="tags:[{{post.tags}}]+id:-{{post.id}}" as |related|}}
{{#if related}}
    <section class="related-wrapper gh-canvas">
        <h3 class="related-title">You might also like...</h3>
        <div class="post-feed related-feed">
            {{#foreach posts}}
                {{> "loop"}}
            {{/foreach}}
        </div>
    </section>
{{/if}}
{{/get}}

What I tried is something like:

{{#foreach post.tags}}
    {{#match name "hash-en"}}
        {{#set "lang_tag"}}hash-en{{/set}}
    {{/match}}
    {{#match name "hash-de"}}
        {{#set "lang_tag"}}hash-de{{/set}}
    {{/match}}
    {{#match name "hash-fr"}}
        {{#set "lang_tag"}}hash-fr{{/set}}
    {{/match}}
    {{#match name "hash-es"}}
        {{#set "lang_tag"}}hash-es{{/set}}
    {{/match}}
    {{#match name "hash-it"}}
        {{#set "lang_tag"}}hash-it{{/set}}
    {{/match}}
    {{#match name "hash-nl"}}
        {{#set "lang_tag"}}hash-nl{{/set}}
    {{/match}}
    {{#match name "hash-pt"}}
        {{#set "lang_tag"}}hash-pt{{/set}}
    {{/match}}
    {{#match name "hash-pl"}}
        {{#set "lang_tag"}}hash-pl{{/set}}
    {{/match}}
    {{#match name "hash-ru"}}
        {{#set "lang_tag"}}hash-ru{{/set}}
    {{/match}}
    {{#match name "hash-ja"}}
        {{#set "lang_tag"}}hash-ja{{/set}}
    {{/match}}
    {{#match name "hash-zh"}}
        {{#set "lang_tag"}}hash-zh{{/set}}
    {{/match}}
{{/foreach}}

{{#get "posts" limit="5" filter="tags:{{@lang_tag}}+tags:[{{post.tags}}]+id:-{{post.id}}" as |related|}}
{{#if related}}
    <section class="related-wrapper gh-canvas">
        {{#match @lang_tag "hash-en"}}
            <h3 class="related-title">You might also like...</h3>
        {{/match}}
        {{#match @lang_tag "hash-de"}}
            <h3 class="related-title">Das könnte dich auch interessieren...</h3>
        {{/match}}
        {{#match @lang_tag "hash-fr"}}
            <h3 class="related-title">Vous pourriez aussi aimer...</h3>
        {{/match}}
        {{#match @lang_tag "hash-es"}}
            <h3 class="related-title">También te puede gustar...</h3>
        {{/match}}
        {{#match @lang_tag "hash-it"}}
            <h3 class="related-title">Potrebbe piacerti anche...</h3>
        {{/match}}
        {{#match @lang_tag "hash-nl"}}
            <h3 class="related-title">Je zou dit ook leuk kunnen vinden...</h3>
        {{/match}}
        {{#match @lang_tag "hash-pt"}}
            <h3 class="related-title">Você também pode gostar...</h3>
        {{/match}}
        {{#match @lang_tag "hash-pl"}}
            <h3 class="related-title">Możesz również polubić...</h3>
        {{/match}}
        {{#match @lang_tag "hash-ru"}}
            <h3 class="related-title">Вам также может понравиться...</h3>
        {{/match}}
        {{#match @lang_tag "hash-ja"}}
            <h3 class="related-title">あなたも好きかもしれません...</h3>
        {{/match}}
        {{#match @lang_tag "hash-zh"}}
            <h3 class="related-title">你可能还喜欢...</h3>
        {{/match}}

        <div class="post-feed related-feed">
            {{#foreach related}}
                {{> "loop"}}
            {{/foreach}}
        </div>
    </section>
{{/if}}
{{/get}}

But that did not work, the element just stays empty.
I had versions with #has that just outputed the related-title like that which worked:

    {{#post}}
        {{else has tag="#en"}}
            <h3 class="related-title">You might also like...</h3>
        {{else has tag="#fr"}}
            <h3 class="related-title">Vous pourriez aussi aimer...</h3>
        {{else has tag="#es"}}
            <h3 class="related-title">También te podría interesar...</h3>
        {{else has tag="#it"}}
            <h3 class="related-title">Potrebbe interessarti anche...</h3>
        {{else}}
            <h3 class="related-title">Das könnte dich auch interessieren...</h3>
        {{/has}}
    {{/post}}

But when I then try to insert the filter block into the has blocks, they just stay empty:

{{#get "posts" limit="5" filter="tags:hash-de+tags:[{{post.tags}}]+id:-{{post.id}}" as |related|}}
{{#if related}}

I think I mess around with the contexts and I just can’t get the correct way.

I could also live with a realy impropper solution like the following, if it would just work, but my brain got wasted already:

{{#post}}
    {{#has tag="#de"}}
        {{#get "posts" limit="5" filter="tags:hash-de+tags:[{{post.tags}}]+id:-{{post.id}}" as |related|}}
            {{#if related}}
                <section class="related-wrapper gh-canvas">
                    <h3 class="related-title">Das könnte dich auch interessieren...</h3>
                    <div class="post-feed related-feed">
                        {{#foreach related}}
                            {{> "loop"}}
                        {{/foreach}}
                    </div>
                </section>
            {{/if}}
        {{/get}}
    {{else has tag="#en"}}
        {{#get "posts" limit="5" filter="tags:hash-en+tags:[{{post.tags}}]+id:-{{post.id}}" as |related|}}
            {{#if related}}
                <section class="related-wrapper gh-canvas">
                    <h3 class="related-title">You might also like...</h3>
                    <div class="post-feed related-feed">
                        {{#foreach related}}
                            {{> "loop"}}
                        {{/foreach}}
                    </div>
                </section>
            {{/if}}
        {{/get}}
    {{/has}}
{{/post}}

Is there a way to find an internal post tag matching something of a list, store that in a kind of variable and use it for match afterwards?
Something like in my first try?

Btw. I also tried to move the language hashtag to the first element of the Tags of the post and tried different combinations of
tags:[{{post.primary_tag}}]
tags:[{{post.primary_tag.slug}}]
tag:{{post.primary_tag}}
primary_tag:{{post.primary_tag}}

in the filter. None of them worked, the list always kept empty.

How you would solve that?

Ghost doesn’t implement a #set helper, as far as I know. So that attempt isn’t going to go anywhere. (As an FYI, if that came from an LLM, they’re generally pretty terrible at the dialect of handlebars that Ghost speaks.)

I’d think about setting up separate collections for each language. That’ll let you do things like {{#is "de"}} do some german stuff {{/is}}

Filtering by tags (filter=“tags:[{{post.tags}}]”) does work, but will get you posts matching all the tags on that post. (And it might need to be tags:[{{tags}}])

Consider committing to the language tag always being the primary tag, so that you can use primary_tag as a filter item. Or even tags.[1].slug, if you always put it second… There are some examples of filtering on tag positions here: A better related block for Ghost

1 Like

Hi @Cathy_Sarisky
Thanks so much for your input and your enlightening blog post.

I already tried a lot of combinations with the primary_tag like (just to name some):

primary_tag:[{{post.primary_tag.slug}}]
primary_tag:[{{post.primary_tag}}]
tags:[{{post.primary_tag.slug}}]
tags:[{{post.primary_tag}}]

None of them worked.
I think that’s due to to how primary_tag works. Without understanding the exact implementation, I think, that primary_tag just offers the first visible tag.

Your hint with the post.tags array finally worked well.

When I use: tags:[{{post.tags[0].slug}}] it works as expected.
Even if thought post.tags[0] should result in the same as post.primary_tag, it isn’t.

So adding this to my filter string results in exactly the wanted behavior.
So I just have to set #en and #de as the first tag and complete:
tags:[{{post.tags}}]+id:-{{post.id}}
to:
tags:[{{post.tags}}]+tags:[{{post.tags[0].slug}}]+id:-{{post.id}}
I now only get recommendations with posts matching the same language tag.

THAAAANKS!

1 Like