How to get taxonomy term of the current page and populate queries in the template

Hm, if you registered a taxonomy for the “page” object type correctly and then assigned a term of that taxonomy to a page… I believe you can then access the taxonomy and term slugs in the following way:

get_query_var( 'taxonomy' )
get_query_var( 'term' )

If you print_r($wp_query) you will see all the parameters that are there when generating a current page that’s displayed. With code above you’re accessing those parameters from $wp_query.

Then to get the term object with full info you can use get_term_by function, like so

$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term->name;

This will print the “nice” name of the term.

I believe if you use get_query_var('term') or $term->slug (after getting the term object) you can use that slug in all of other queries.

Hope that helps. I never used taxonomy for pages.
Let me know how you get on.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)