13

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!

James
  • 151
  • 1
  • 2
  • 6

9 Answers9

11

A possible solution:

$taxonomy = get_queried_object();
echo  $taxonomy->name;
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
6
get_query_var('taxonomy');

Just this should work.

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
Victor Teixeira
  • 291
  • 1
  • 2
6

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;
chodorowicz
  • 1,062
  • 1
  • 15
  • 19
5

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)
Jan Fabry
  • 7,221
  • 2
  • 36
  • 41
3

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).

gilzow
  • 31
  • 1
3

I know this is a solved problem but here's another way to get the taxonomy name which I think is clean. For those finding this just now as I did. I like to promote awareness of global variables in wordpress.

$tax_term = $wp_query->query_vars['tax_name'];
DeviousBlue
  • 241
  • 4
  • 15
  • 1
    Just 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'] – Mere Development Mar 07 '12 at 16:17
2

You can get specific term / taxonomy data like name, description etc on taxonomy.php

<?php 
$termid = get_queried_object()->term_id;
$term = get_term( $termid, 'taxonomy-name' );
echo $term->name.'<br>';
echo $term->description;
?>
Shashank Sharma
  • 585
  • 3
  • 14
  • Downvoting as `$taxonomy = get_queried_object();` already returns the $term you're looking for, no need to execute that extra query by ID. Thanks though. – ecairol Sep 13 '20 at 02:36
0

Someone might step this way (I did) so...

$tax = $wp_query->get_queried_object();
$tax_term = $tax->name;
$tax = get_taxonomy($tax->taxonomy);
$taxonomy =  $tax->label;
echo "$tax_term are $taxonomy";

returns "Lemons are Fruit".

I'd love to know a one liner for this but I don't think there is one, you need parts of two objects.

Chris Pink
  • 1,558
  • 1
  • 15
  • 28
0

As of Wordpress version 3.1.0 for single_term_title() and Wordpress version 4.9.2 for term_description(), both can be used to display the terms name and description. Here is an example.

<hgroup>
<h1><?php single_term_title(); ?></h1>
<h2><?= term_description(); ?></h2>
</hgroup>

Alternatively you can use get_the_archive_title() to display the taxonomy name and curren associated term. A typical result would be: Taxonomy: Term.


More

amarinediary
  • 4,930
  • 4
  • 27
  • 45