How to add descriptions to category and tag pages in WordPress


WordPress gives us a great opportunity to add descriptions to almost all elements in our website. In the tutorial today, we will learn how to deal with category and tags descriptions and how to make our tag and category pages better SEO optimized. In other words, this is about custom category page in wordpress.

How to add tag and category descriptions

The first part of the tutorial is focused on preparations. To do so, we have to add a few categories to our WordPress instance and a few tags assigned to our posts. Here is how to do it:

Login to wp-admin section and click Categories under Posts menu. There you will find Add New Category option. Now add a category name, slug and short description which will later be used for SEO purposes.

The same way find Tags menu under Posts tab and add a few tags making sure they have names, slugs and descriptions up to 100 characters.

How to build tags and category pages

After these changes we are ready to start building our tags and category pages. In the tutorial named How to create WordPress theme from scratch we learned how to build a simple WordPress theme. If you followed its steps, you have the theme files and you can just copy the code of single.php file to tag.php and category.php. Note that these two files handle the requests towards tags and category pages. The loop inside these two files will remain the same. We can easily optimize it further, but today we are focuses on descriptions and the default code would do the job.

How to optimize tags page

Before making our tag page better SEO optimized, let’s make a little test to see what WordPress knows about our tags. To do so we will print the information that comes from WordPress using print_r. Here is what needs to be done.

Open tag.php file and right after

<div class="content rounded">

add the following piece of code:

<?php

$check_tags = get_tags();

echo '<pre>';

print_r($check_tags);

echo '</pre>';

?>

The result would be similar to this one:

WordPress tag objects

WordPress tag objects

In my case I see two objects in an array and each one represents one of my tags. As you can see they have names, slugs taxonomies, IDs, descriptions and so on. Our task is to grab the names and descriptions. We can then print them in our page or in head section as page descriptions. This presumably would increase our SEO score or at least the general rules say so. From this point on, we have two options to extract the names and descriptions. We can either loop through the objects or use some built in functions. Luckily we have handy functions that would help in this case. Let’s replace:

<?php

$check_tags = get_tags();

echo '<pre>';

print_r($check_tags);

echo '</pre>';

?>

with:

<p>Our tag is: <?php single_tag_title(); ?></p>

<?php if (is_tag()) {

            echo tag_description();

}

?>

and refresh the tag page. Now instead of seeing the objects, we see only the name of the tags we are reviewing and its description. Here is an example:

WordPress tag description

WordPress tag description

So far we managed to obtain the elements we need and we can now proceed and add then in head section of our WordPress instance. The head section is defined in header.php file which I part of our theme. To apply the modification, we need to open the file and add the following code before the closing head tag as shown below:

<?php if (is_tag()) {

            $tag_description=tag_description(); ?>

            <meta name="description" content="<?php echo  strip_tags( $tag_description); ?>" />

<?php } ?>

That code says if the current page is tag page and the tag in question has description, print it as meta name description. Do not forget to save and close the file and then refresh the page. Then check its source code and you will find the tag description printed as meta name description. In my case the relevant piece of the source code is as follows:

<meta name=”description” content=”this is the description of mytag ” />

where ‘this is the description of mytag’ exactly matches the description I added to my tag. In some particular cases, the tag description may come wrapped with HTML elements, so we use strip_tags function to remove them from the string. This way we get only the text element of the description.

Custom category page

The approach we will apply to our category pages is fairly similar to the above described one. As a first step here, we will again make a test with simple variable and print_r option. Let’s open category.php page add:

<?php $category = get_category( 3);

echo '<pre>';

print_r($category);

echo '</pre>';

?>

right after :

<div class="content rounded">

With this piece of code we get all the elements related to a given category. The expected result is shown below:

WordPress category parameters

WordPress category parameters

From all these elements we need to extract the description. Later we will display it on the page and in head section. Now we can change:

<?php $category = get_category( 3);

echo '<pre>';

print_r($category);

echo '</pre>';

?>

to:

<?php if (is_category()) {

 $description=category_description();

 echo $description;  

 } ?>

The code checks if the page we are on is category page and then defines a new variable equal to category description and finally prints it. The expected result is similar to the following one:

WordPress category description

WordPress category description

Generally speaking we can get the category name as well, but according to our theme functionality, this is not needed. The name is already shown in the menu and breadcrumbs so the visitors know where they are.

The final part of the tutorial is focused on category page SEO stats. To improve them by adding description to the page, you should open header.php file and add the following pieceof code before the closing head tag:

<?php if (is_category()) {

  $cat_description=category_description();?>

  <meta name="description" content="<?php echo  strip_tags( $cat_description); ?>" />

<?php } ?>

The code says if the page we are on is category, then set a custom variable, make it equal to category description and then print it as meta description. After this modification, you can refresh any category page and check its source code. There you will find the result of the code we added to our theme. In my case the category page description is as follows:

<meta name="description" content="Description of category 2"/>
Summary
Custom category and tag pages in wordpress - how to add descriptions
Article Name
Custom category and tag pages in wordpress - how to add descriptions
Description
How to customise wordpress pages - custom category and tag pages. How to add description to tags and category pages - detailed tutorial
Author
Publisher Name
wppotion


Do you want to share your opinion?

Your email address will not be published. Required fields are marked *


*

We are not industry specific. We are WordPress specific. We work with everyone to help them find solutions with their troubles.
If you show us the problem you are experiencing, we will show you how to fix it. It is that simple.


Reported WordPress issues

  1. Wordpress Rest API showing theme custom fields within the end point url are refusing to update via a post command
  2. Generate new post WordPress based on MySQL database
  3. Iframe (ads) is blur in Chrome when it is resized to fit in sidebar div
  4. How to make a WordPress page mobile responsive?
  5. Permalink Settings You should update your web.config now [on hold]
  6. how to upload image to wordpress with flutter using REST API
  7. Setting CSS property - filter: blur(x) with JS for site-inner class within genesis framework; Cannot read property 'getAttribute' of null
  8. Error on wordpress on my VPS: Your PHP installation appears to be missing the MySQL extension which is required by WordPress [on hold]
  9. Netlify deploy with results of TypeError: Cannot read property 'replace' of undefined
  10. How to position text at the same vertical level across columns

WordPress problems we are working on

  1. How to target a specific group on Buddypress (WordPress) to show it on a widget?
  2. Is it possible for Month names to be shown in my language
  3. Displaying and grouping posts
  4. Wordpress custom endpoints (WP_REST_Controller) 404 only on mobile
  5. Set default value in database [on hold]
  6. How do I programmatically set a user as spam in BuddyPress?
  7. Can i edit styling of form in ultimate member wordpress plugin?
  8. wordpress product attribute not creating automatically only work size and color other attributes not work need to add manually again same attribute
  9. Uncaught Error: Syntax error, unrecognized expression on WordPress website
  10. Wordpress website hacked and .htaccess file is creating automatically with strange code

Resolved issues

  1. Wordpress gutenberg editor gallery first image missing [on hold]
  2. how to fix the error No Input file specified on wordpress when using .php extention on pages
  3. How do I integrate Chargebee api to wordpress?
  4. Sorting a wp_query with more than one 'orderby'?
  5. Wordpress API: Sort posts by sticky and date
  6. Column background color not showing up
  7. How to show a row for each column?
  8. Wordpress site's frontend css styles not loading [on hold]
  9. Get the just added term_id (WordPress)
  10. Permalink structure does not get updated
wppotion - powered by persistence and passion