Accessing primary_tag within a post context

My theme has a main page that lists posts by tag group.

I want to use the primary_tag feature_image as a fallback when the post feature_image doesn’t exist.

So I’m operating within a post context as it constructs the image, title, excerpt etc.

I tried this:

{{#primary_tag}}
{{#if feature_image}}
     code to output the image
{{/if}}
{{/primary_tag}}

Am I trying to do the impossible? This Post Context Doc says the primary_tag is a path expression, which points to a whole tag object, rather than a helper function.

But it’s clear that if statement is never returning true and outputting the image.

In this snippet, you’re checking whether the tag’s feature image exists, not the post’s. Is that what’s intended? If so, then this code will work, displaying a featured image when there’s a primary tag and that tag has a feature image.

If you want to fall back to the tag’s image when the post doesn’t have a feature image, then the snippet should look like this:

{{#post}} 

{{#if feature_image}}
  {{!  code for when there's a feature image for the post }}
{{else}}
  {{#primary_tag}}
    {{#if feature_image}}
      code to output the image
    {{/if}}
  {{/primary_tag}}
{{/if}}

{{/post}}

You got what meant exactly right, and what you wrote out is what I did, but It failed to work.

If it should work, then maybe I had something odd going on and I need to take another crack at it. Thanks for your help. I’ll persevere.

In this type of scenario, the log helper can be a great tool for seeing what’s happening:

1 Like