How to check if each post in a loop has a tag

Hello, I use

<Loop type=post orderby=date order=desc paged=12>
<a href="{Field url}"><img class="alt-loop" width="500" height="400"
     src="https://mysite.com/wp-content/uploads/alternative/{Field alternative_image /} "
    </a>
<a href="{Field url}"><Field title /></a>
</Loop>

and I want to test if each post has tag “one-tag” and, if so, show another image than the one defined here. But I did not find anything that worked.

It took some time searching in the documentation, but I found this page:

The example uses <If loop..> to check if the post has a specific category. It creates a taxonomy term loop with the current post and the matching category name.

<If loop type=taxonomy_term taxonomy=category include="term-name" post=current>

For post tags it should work similarly. The type=taxonomy_term is not necessary when using the loop parameter taxonomy. (Reference)

<If loop taxonomy=tag include=one-tag post=current>
  Conditionally display this stuff
<Else />
  Doesn't have the tag
</If>
1 Like

I found the article and tried the code… but it does not seem to work - and for some reason, the initial images do not show if I add the code.

Here’s another way using List.

Inside the loop, create a list of post tags associated with the current post.

<List name=tags>
  <Loop taxonomy=tag post=current>
    <Item><Field name /></Item>
  </Loop>
</List>

Check if the list includes a matching term.

<If list=tags includes value=tag-1>
  Yes
<Else />
  No
</If>

for some reason, the initial images do not show if I add the code

Not sure why the shorter version with If loop is not working there. It sounds like it’s affecting the parent post loop. I’ve made a note to look into it.