Display Meta Description Tag Content for Post Categories

Meta tags are one of the best way to provide important information about your site. It helps the search engines to crawl information. Even though one might argue that it’s not the meta tags rather the content and the keywords which are more important now a days for SEO.

Well, I say, it doesn’t hurt me to add two or three extra lines on my site which will increase the chances for my site to provide more information and possibly get those information picked up by some search engines. Trust me, this argument will go on and on.

Now, there are so many meta tags that we can use on our site like “title”, “description”, “keywords”, “author” etc. For today, we will focus on “description” meta tag and will learn how easily we can add dynamic content for Categories in WordPress.

By default a meta tag would look something like this:

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

Lets assume that you have three different post category on your site. News, Reviews, and Blogging. Now, all of your category has its own home URL, like for “News” it should be something like this:

https://www.urdomain.com/news/

or like this:

https://www.urdomain.com/category/news/

Depending on the “Permalink structure” that you have assigned. Same pattern should followed by rest of the categories as well. Now, you can go to any one of your category home page and take a look at the source code of your page. Do you see the meta description tag within <head></head> tag? What do you see the values for “content” attributes? Is the content value changing as you change the category? Most probably your answer would be “No”. So, keep reading.

Adding Category Description

Before we talk about the snippet code, we need to add some description for some of our category from WordPress admin panel. Afterwards, we will focus on the snippet. To add description on your categories, follow these steps:

Step 1: Login to your WordPress site.
Step 2: Hover you cursor on Posts from sidebar and then click on Categories from the sub-menu.
Step 3: On the right hand side of your screen, hover on any category and the click on Edit.
Step 4: Edit Category page will open. Add some information on the Description text area.
Step 5: Once done, simply click on Update button.

You should see the categories with description like this from your “Categories” page.

Category Description in WordPressSample Screenshot of Categories with Description.

You are done with adding category description that we will be displaying on our meta tag. Follow those steps for every single category that you have on your site.

Magic of Snippet

At this stage I am assuming you have added all of your post category description. Let’s write the function and retrieve our category description from the database. Here we go.

<?php
function dynamic_category_desc() {	
   if (is_category()) {
      return category_description();
   } else {
      return bloginfo('description'); } }
?>

What this code does is, it will grab the “category description” that you just added from the admin panel for each category while you are on any category’s home page. For rest of the URLs it will simply show the site’s description. Fair enough? Well, you can actually get really creative with this function but to keep our discussion simpler we will stay focused on categories only. Now, let’s add this code on your template’s functions.php page. To do so follow these steps:

Step 1: Click on Appearance menu from the admin panel and then click Editor.
Step 2: Edit Themes window will appear. Find functions.php file from the right hand side of your screen and click on it.
Step 3: Now simply copy and paste the snippet mentioned above on your page.
Step 4: Hit Update File button once you are done.

We got one more thing to do and then you are done with this tutorial. Now we have to add the following line on your template’s header.php page. It should be anywhere in between the <head></head> tag.

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

We are simply adding the description meta tag and calling our function to display the data. Bam! you are done.

Note: Data for content attribute may or may not show the category description wrapped with <p></p> tag. If it doesn’t then there is nothing to worry about. If it does, simply add the following line anywhere on your theme’s functions.php page.

remove_filter('category_description','wpautop');

You are all set. As I mentioned earlier, you can make this code even more dynamic by using some Conditional Tags. Like description for single post page or for search result page etc. but that’s your call to make. Now I am taking questions.

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.