admin管理员组文章数量:1122832
I have looked to see if there is a simple function that only returns the name of a category on a post. I am looking to output it as a subheading for a list of posts but do not want it as a link and the the_category();
function only returns a link.
I also don't want it as a list. Any thoughts on how I can just get the "slug" of the category?
I have looked to see if there is a simple function that only returns the name of a category on a post. I am looking to output it as a subheading for a list of posts but do not want it as a link and the the_category();
function only returns a link.
I also don't want it as a list. Any thoughts on how I can just get the "slug" of the category?
Share Improve this question edited Aug 20, 2017 at 0:36 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 20, 2017 at 0:31 Chris ThomasChris Thomas 234 bronze badges2 Answers
Reset to default 0What you are looking for is get_the_category()
. Retrieve a list of the categories, and then run a loop and only output their names:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach( $categories as $category ){
echo esc_html( $category->name );
}
}
This function returns an array of WP_Term
objects. You can check the provided link for a list of available methods.
You can use get_the_terms with any taxonomy and output without the link like this :
$terms = get_the_terms( get_the_ID(), 'category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$cats = array();
foreach ( $terms as $term ) {
$cats[] = $term->name;
}
$cats = join( ', ', $cats );
echo $cats;
endif;
}
本文标签: child themeCalling Category name without the link
版权声明:本文标题:child theme - Calling Category name without the link 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288878a1928187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论