开发者

Wordpress - Get name of Category a post belongs to?

开发者 https://www.devze.com 2022-12-26 03:39 出处:网络
in Wordpress on a post how can I g开发者_开发问答et the name of the category it belongs to (each post will only have one category on my website).The version of wordpresss you\'re using is very much re

in Wordpress on a post how can I g开发者_开发问答et the name of the category it belongs to (each post will only have one category on my website).


The version of wordpresss you're using is very much relevant for your question, but a good place to start would be the wordpress codex where you can find for example the documentation of this function.

It is also important to know whether your code will be placed within the infamous loop (which would be the case if you're editing the post template) or not (for example, if your code goes at the sidebar, header, etc).

Assuming the first case, and taking the example from the manual, you could use:

<?php
$categories = get_the_category();
// there is only one, so extract the first from the array 
$category = $category[0];
?>


These can go in header.php for generating meta tags, page titles, etc.

<?php echo trim(strip_tags(category_description())); ?>

<?php echo single_cat_title(''); ?>

Also see Template Tags/single cat title « WordPress Codex

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号