如何在没有任何标记的情况下打印Wordpress帖子的类别,只是纯文本


How to print a Wordpress post's category without any markup, just plain text?

我正在创建一个wordpress主题,主题中的每个类别都有一个指定的颜色。 我想要 wordpress 代码,只需将类别的名称打印到帖子的 id 中,以便可以将其设置为相应的颜色,但是我尝试的每个代码都会给出 html 标记,这会弄乱我的代码。 以下是我希望它的工作方式。

<article> <!-- POST -->
<h1>Post title</h1>
<h2 id="***Here is where I want to insert the code to print the post's category id***>Category Name (Same code as in the ID)</h2>
<img *featured image*>
<p>blah blah article summary</p>
</article>

任何帮助将不胜感激!

我想你想在你的代码中这样做......

 <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>

假设每个帖子只有一个类别,应该可以正常工作。

您可以使用

<?php single_cat_title(); //prints category in plain text ?>

食品

<?php the_category(' '); //displays category with link ?>

食品