Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by mi@mgsh.com.cn

Get the tag ID and alias of the current tag page of the website CMS

In the theme development of website production, what should I do if I want to get the ID and alias of the current tag on the tag article list page?This can be achieved by using the get_term_by() function by the label name,get_term_by()Functions can return information such as the name, alias, and ID of a category or label.

if(is_tag()){ $tagName = single_tag_title('',false); $tagObject = get_term_by('name',$tagName,'post_tag'); $tagID = $tagObject->term_id; }

Code meaning: On the tab page, choose to get the tab name of the current tab page, use the tab name to passget_term_by()The function returns an array value, and then you can get the ID of the current tag in the array.

Expansion:

It can also be written as a function and placed in the functions.php file of the theme (choose one of the following codes)
Code one:

function get_tag_id() { $tagname = single_tag_title('',false); $tag = get_term_by('name', $tagname, 'post_tag'); if($tag){ return $tag->term_id; }else{ return 0; } }

Code two:

function get_tag_id() { $current_tag = single_tag_title('', false); $tags = get_tags(); foreach($tags as $tag) { if($tag->name == $current_tag) return $tag->term_id ; } }

calling code:

// call code

 

Back to Top