admin管理员组文章数量:1129457
I'm trying to show the category name in my category.php but it's not working when I have posts assigned to multiple categories as it chooses the first category.
<h1><?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h1>
So I tried by using another method which is to show the category name using the slug URL but that shows nothing.
<h1><?php $cat = get_category_by_slug( $slug );
echo $cat->name; ?></h1>
Output is <h1></h1>
Basically, I want someone opening example/category/team
to see <h1>Team</h1>
or example/category/projects
to see <h1>Projects</h1>
irrespective of the posts being assigned to multiple categories.
What am I doing wrong?
I'm trying to show the category name in my category.php but it's not working when I have posts assigned to multiple categories as it chooses the first category.
<h1><?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h1>
So I tried by using another method which is to show the category name using the slug URL but that shows nothing.
<h1><?php $cat = get_category_by_slug( $slug );
echo $cat->name; ?></h1>
Output is <h1></h1>
Basically, I want someone opening example.com/category/team
to see <h1>Team</h1>
or example.com/category/projects
to see <h1>Projects</h1>
irrespective of the posts being assigned to multiple categories.
What am I doing wrong?
Share Improve this question asked Nov 19, 2023 at 5:08 user3362364user3362364 1032 bronze badges1 Answer
Reset to default 1For the heading on a category archive use either:
the_archive_title();
Or:
single_term_title();
The first one can be used on the template for any archive, while the second should only be used on category, tag, or taxonomy archive templates.
You shouldn’t need to get anything from any of the individual posts. They may have other categories and the category relevant to the current archive might not be the most relevant.
本文标签: Show category name in categoryphp when posts assigned to multiple categories
版权声明:本文标题:Show category name in category.php when posts assigned to multiple categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736744726a1950709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论