I'm creating a custom tag.php page for a WordPress theme.
I need to get the name, id and slug for the tag page that I am one (i.e. www.site.com/tag/fruit/ )... I see tons of examples online about how to list the tags for a page/post, but none that say how to easily get the name, id and slug for a specific tag page.
I tried to use the following... but the code below is for cycling through all the tags in a post. I just need the one tag for that tag page. If I try the option below, I don't get the correct tag (it picks some other tag on my site).
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag_id = $tag->term_id;
$tag_name = $tag->name;
$tag_slug = $tag->slug;
}
}
echo "<!-- tag_id: ".$tag_id." -->";
echo "<!-- tag_name: ".$tag_name." -->";
echo "<!-- tag_slug: ".$tag_slug." -->";
Any help is appreciated. Thanks
On your tag page, and for that matter, any taxonomy/term page, you can get the info from the current term being viewed with
get_queried_object()
. This will return the whole term object.If you just need the ID, you can use
get_queried_object_id()
Example: