Get the currently selected post category or tag

The two WordPress templates to handle category and tag archives are called category.php and tag.php respectively.

You can obtain the currently selected category term by using the following code:

$term = get_term_by('id', get_query_var('cat'), 'category' );

To obtain the currently selected tag term use the following code instead:

$term = get_term_by('id', get_query_var('tag'), 'tag' );

The $term variable should now have the following structure:

stdClass Object
   [term_id] => 1
   [name] => General
   [slug] => general
   [term_group] => 0
   [term_taxonomy_id] => 1
   [taxonomy] => category
   [description] => 
   [parent] => 0
   [count] => 3

Bon App├ętit!

Tagged with: