如何在 Wordpress 中的帖子中获取类别标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/945906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:30:31  来源:igfitidea点击:

How to get the category title in a post in Wordpress?

wordpressgetcategories

提问by Keith Donegan

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?

假设我在 Wordpress 中有一篇名为 Hello World 的帖子,并且我正在直接查看此页面,我将如何查找“Hello World”的类别并显示它?

回答by RichieHindle

Use get_the_category()like this:

get_the_category()像这样使用:

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

It returns a list because a post can have more than one category.

它返回一个列表,因为一篇文章可以有多个类别。

The documentationalso explains how to do this from outside the loop.

该文档还解释了如何从循环外执行此操作。

回答by Joseph

You can use

您可以使用

<?php the_category(', '); ?>

which would output them in a comma separated list.

这将在逗号分隔列表中输出它们。

You can also do the same for tags as well:

您也可以对标签执行相同的操作:

<?php the_tags('<em>:</em>', ', ', ''); ?>