How to get the category title in a post in Wordpre

2019-04-03 08:16发布

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?

2条回答
淡お忘
2楼-- · 2019-04-03 08:48

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>', ', ', ''); ?>
查看更多
聊天终结者
3楼-- · 2019-04-03 09:09

Use get_the_category() like this:

<?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 documentation also explains how to do this from outside the loop.

查看更多
登录 后发表回答