Reputation: 1900
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.
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.
Upvotes: 9
Views: 17846
Reputation: 1900
OK i have found the solution.
I set on the get_term_by the taxonomy 'post_tag' instead 'product_tag' as it woocommerce tags. Next time i will look carefully on the url of the wp-admin when i am looking on taxonomy page:
...wp-admin/edit-tags.php?taxonomy=product_tag&post_type=product
So the code should be like that:
$tag = get_term_by('slug', $slug, 'product_tag');
$tag_name = $tag->name;
<h1><?php echo $tag_name; ?></h1>
Solved.
Upvotes: 5
Reputation: 462
$tag = get_term_by('slug', ' hedoms','post_tag');
$tag_id = $tag->term_id;
Upvotes: 11