Reputation: 3
I'm using this code to display the category title on the Archives page of my website.
<h2><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h2>
The above code works perfectly for displaying the category title.
Eg: website.com/category/question
On this page the h1
title is 'Question', which is right!
But the issue I'm facing, it doesn't display the tag title. Instead, it only displays the Category title on the tag page.
Eg: website.com/tag/howto
On this page the h1 title remains 'Question', which is wrong! I want it to display the H1 title "eg. How-to"
Is there any way to modify the above code to display the tag title as well?
Upvotes: 0
Views: 111
Reputation: 5659
The most basic solution would be to get_queried_object
instead of the Category. Then use the property name
<h2><?php $object = get_queried_object(); echo $object->name; ?></h2>
You could also do something specific for a tag like this.
<?php
$object = get_queried_object();
if (is_a($object, 'WP_TERM')){
if ($object->taxonomy === 'post_tag') {
// Do something specific for a tag
echo $object->name;
}
}?>
Or simply use
is_tag()
or is_category()
to determine what you want to do.
Upvotes: 1