How to Show the Current Taxonomy Title, URL, and more in WordPress

405

Recently one of our users reported an issue with their theme. They had a share button on all pages including the archive pages. However, the taxonomy archive page share buttons were showing the information of the most recent post instead of the actual taxonomy archive. We took a look at their code just to find out that they were calling the_permalink(); and the_title(); tag which are reserved for posts not archive pages. In this article, we will show you how to get the current taxonomy title, url, and other information on the specific taxonomy archive page.

All you have to do is paste the following code on your taxonomy archive page.

  $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );   

This gets the information of the current taxonomy based on the archive page you are on. For example, if you were on a category page called “business”, then it will get the information for that taxonomy.

After that line of code, you can use it to display the title of the taxonomy and other info like this:

  echo $term->name; // will show the name  echo $term->slug; // will show the slug  

You can use it for the all of the following values:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

We hope that this will help other theme designers who might need this. It can come in handy to show the current taxonomy title or any of the other information.