Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Get tag id by slug

I am trying to get id\name of tag by it slug.

Thats my code:

$tag = get_term_by('slug', 'hedoms', 'post_tag');
$tag_id =  $tag->term_id;

<h1><?php echo $tag->name;?></h1>

I took it from here: https://codex.wordpress.org/Function_Reference/get_term_by

I have tag with the slug hedoms but the <h1> is empty, it not returns the value. I have tried this function (get_term_by) with category - and same result.

enter image description here

the 2nd field is the slug.

But if i do it with id and not slug, it works fine:

$tag_id = get_term_by('id', 97, 'post_tag');
echo $tag_id->name;

It look like the get_term_by recognize only 'id' as field.

What i miss here? it should be on loop or something ?

The file i trying to do it is archive-product.php of Woocommerce.

like image 562
Oshrib Avatar asked Jun 22 '17 20:06

Oshrib


People also ask

How do I find the tag ID in WordPress?

Now you can find your WordPress tag ID also in a similar way. Visit Posts » Tags and then hover the mouse over the tag whose ID you want to view. Once done, your tag edit URL will appear at the bottom left corner of the screen.

Is WordPress a tag?

WordPress tags is one of the tools you can use to group your posts, based on similar details. Usually, tags are located under a post or in the sidebar. When a visitor clicks a particular tag, WordPress will open an archive page (tag page) – indexing all the posts and custom post types that have the same tags.

How do I see all tags in WordPress?

get_tags( string|array $args = '' ): WP_Term[]|int|WP_Error. Retrieves all post tags.


1 Answers

$tag = get_term_by('slug', ' hedoms','post_tag');

$tag_id =  $tag->term_id; 
like image 131
mayank Avatar answered Sep 19 '22 00:09

mayank