Wordpress : How Do I get taxonomy name in taxonomy.php?
Solution 1
A possible solution:
$taxonomy = get_queried_object();
echo $taxonomy->name;
Solution 2
For my taste is overly complicated, but here it goes:
$term = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
echo $term->name;
Solution 3
get_query_var('taxonomy');
Just this should work.
Solution 4
If you check $wp_query->get_queried_object()
on a taxonomy page, this will contain the term object, which has a reference to the taxonomy identifier (in my example it's replymc_people
). Pass this to get_taxonomy
, and you get the full taxonomy object.
object(stdClass)[325]
public 'term_id' => string '113' (length=3)
public 'name' => string 'Jef Staes' (length=9)
public 'slug' => string 'jef-staes' (length=9)
public 'term_group' => string '0' (length=1)
public 'term_taxonomy_id' => string '107' (length=3)
public 'taxonomy' => string 'replymc_people' (length=14)
public 'description' => string '' (length=0)
public 'parent' => string '0' (length=1)
public 'count' => string '3' (length=1)
Solution 5
I know this is answered, but for those who might wind up on this page when searching...
global $taxonomy,$term;
$taxonomy will now contain your taxonomy name ('fruit' from the OP's example) and your taxonomy term name ('lemon' from OP's example).
James
Updated on July 05, 2022Comments
-
James almost 2 years
I am able to display the term of the taxonomy in the taxonomy page, but how do I get the taxonomy , or display the taxonomy on the page.
for example, when I have a taxonomy called "fruit" and I click on a fruit term called "lemons", How do I display both "lemons" and "fruit" on the taxonomy term page?
Just looking for the get term equivalent. Thx!
-
Mere Development about 12 yearsJust to help anyone else looking, yes this does give 'lemons' (using the OP's example), but to get 'fruit' you can use $wp_query->query_vars['taxonomy']
-
ecairol over 3 yearsDownvoting as
$taxonomy = get_queried_object();
already returns the $term you're looking for, no need to execute that extra query by ID. Thanks though.