is_single()
and is_singular()
are not the correct functions to use here.
Your comment mentions that this is being added to search.php
, but is_single()
and is_singular()
will always be false on search.php
, because those functions are checking if the current page is a single post or page, but search.php
is not. It’s is a list of multiple results. is_single()
is not checking the current item in a loop, it’s checking, essentially, what is represented by the current URL.
This is why it’s displaying for ! is_single()
, because is_single()
is false
, and ! false
is true
.
If you need to determine the post type of a post within a loop, you can use get_post_type()
:
<?php if ( 'post' === get_post_type() ) { ?>
<div class="cat-container">
<a class="post-cat bg-darkpurple" href="https://wordpress.stackexchange.com/questions/357660/<?php echo $category_link ?>"><?php echo $category_name ?></a>
</div>
<?php } ?>