is_single() 和is_singular() 此处使用的函数不正确。
您的评论提到,这将添加到search.php, 但是is_single() 和is_singular() 将始终为false onsearch.php, 因为这些函数正在检查当前页面是单个帖子还是页面,但是search.php 不是。这是一个包含多个结果的列表。is_single() 不是检查循环中的当前项,而是检查当前URL所表示的内容。
这就是为什么它显示为! is_single(), 因为is_single() 是false, 和! false 是true.
如果需要确定循环中帖子的帖子类型,可以使用get_post_type():
<?php if ( \'post\' === get_post_type() ) { ?>
<div class="cat-container">
<a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
</div>
<?php } ?>