Display Meta Description Tag Content for Custom Taxonomy Term

In one of my previous post, I discussed how to register a custom taxonomy and I also showed how to assign it to a Custom Post type. In any case, you can take the advantage of a custom taxonomy and can use them as category or anyway you want.

Recently I wrote a post on how to display display dynamic meta description for Post categories. In this post I will focus on the custom taxonomy terms which works pretty much the same way. The only difference is, we would be retrieving the taxonomy term’s description instead of default post categories in WordPress.

Our focus would be on the following meta tag just like before.

<meta name="description" content="{values}">

For sake of better understanding, let’s assume that you already registered a custom post type called “books” on your site and you registered a custom taxonomy “bookcat” to generate various Book categories. I am also assuming you created few terms and added their description just like the one on this screenshot.

Custom Taxonomy Term DescriptionCustom Taxonomy Terms with Description Associated with Books Post Type.

Now, as you can see here I only have one post associated with “History” term. In order to see all the “Books” type post under “History” term, you need to have $taxonomy-bookcat.php or $taxonomy.php template on your theme (see template hierarchy). In my case it was the $taxonomy.php page to show the archive of any taxonomy term. This is exactly where our magic function would be at work.

Now, let’s write the function to retrieve the taxonomy term’s description.

<?php
function taxonomy_term_desc() {
if (is_tax('bookcat')) {
 $t = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy'));
 return $t->description; } }
?>

On this function, I used is_tax conditional tag to check if the browser is showing the archive page for “bookcat” taxonomy. If our condition is true or if the browser is showing the archive page of any “bookcat” taxonomy term, this function would perform rest of the operation. Using the get_term_by function, I am retrieving the current term and it’s data saved into the database. I also used get_query_var function to figure out which taxonomy and what term of this taxonomy is currently being queried. At the end, I am just returning the description of the term. This is as simple as it can get.

Let’s Implement Our Function

The snippet or the function mentioned above should be on your theme’s functions.php file. So, simply copy and paste it on that page and update it. Now, let’s go back to the header.php page and find out the description meta tag. Within the meta tag, call the function like this.

<meta name="description" content="<?php echo taxonomy_term_desc(); ?>">

That’s all. From now on, you taxonomy term’s archive page should display the term’s description. Hope that helps, thank you.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.