Wordpress - Get tag id by slug

admin

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

Thats my code:

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:
<a href="https://codex.wordpress.org/Function_Reference/get_term_by" rel="noreferrer">https://codex.wordpress.org/Function_Reference/get_term_by</a>

I have tag with the slug <strong>hedoms</strong> but the
Code:
&lt;h1&gt;
is empty, it not returns the value. I have tried this function (get_term_by) with category - and same result.

<a href=" " rel="noreferrer"><img src=" " alt="enter image description here"></a>

the 2nd field is the slug.

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

Code:
$tag_id = get_term_by('id', 97, 'post_tag');
echo $tag_id-&gt;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
Code:
archive-product.php
of Woocommerce.