How to insert HTML if a specific tag is present?

Hello.
How can I insert an HTML element if a post has a specific tag that I need?
For example, I would like to insert a <span class='new'>New</span> to my template if the post I’m fetching has the tag ‘new’.

You can use an if statement:

<?php
if($post->tag() == 'new'): ?>
 <span class='new'>New</span>
<?php endif ?>
1 Like

@texnixe
I tried your code but doesn’t seem to work. I’ve changed the variable $post to $item which is what Im using but nothing is being inserted.

What is your field called and what type of field is that?

I’m using the field tag, and I named it tag as well.

Edit:
Sorry, I named it tags.
Now it’s working.
Thanks again.

Ahh dumb me.

Just in case: If you use multiple tags per post, the code above will not work, then you need to check if the tag is in the array of tags:

<?php
$tags = $post->tags()->split(',');
if(in_array('new', $tags): ?>
  <span class='new'>New</span>
<?php endif ?>
1 Like