Category description with multiple categories and get_the_category()

In this follow-up to a previous WordPress tutorial on how to display the Category description using get_the_category(), I look at how to deal with the scenario where posts are assigned to more than one category.

A quick recap

In the previous article I showed how to use the WordPress function get_the_category() to display the category name and category description at the top of the category archive page, so that when the user clicks when of the categories in the menu bar, the category name and category description is dynamically added to the top of the category archive page.

Method when posts are only assigned to one category

Here is the code we used previously in category.php or archive.php:

<?php
if (have_posts()) :
   	if ( is_category() ) {
       	foreach (get_the_category() as $category); ?>
       	<div>
	       	<h2><?php echo $category->cat_name; ?></h2>
    	   	<p><?php echo $category->category_description; ?></p>
       	</div>
	<?php }
    while (have_posts()) : the_post(); ?>

Why this doesn’t work with posts assigned to multiple categories

The problem with the above method is that it only works properly if posts are only ever assigned to one category. If posts are assigned to more than one category, the code only finds the first member of the $category array, which is problematic if the first member of this array is NOT the category which was clicked by the user of your site. For example:

  • let’s say you have a post assigned to category ID=2 and category ID=3
  • your site visitor clicks the link for category ID=2
  • the category archive page loads and displays the name and description of category ID=3
  • Oops! Wrong category name and description. Result – confused site visitor

This happens because the function grabs the data from the database in category ID number order. Taking our example again, let’s say this time the site visitor clicks the category ID=3 menu link, the user will see the correct category name and description – but only because the first member of the $category array happens by chance to be the correct category. Clearly this is too hit or miss, and therefore another method is needed.

Dealing with multiple categories

In order to get our code to work we need to do three things:

  • Determine which category link has been clicked by the site visitor
  • Look through the $category array and find the category which has been clicked
  • Once we have a match, display the category name and description

The code required (take two)

This code can be used in category.php or archive.php. Of course, the use of the is_category() Conditional Tag is a bit redundant in category.php, but let’s keep things simple and multi-purpose!

Find the start of the Loop in category.php, which should look something like this:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

And replace this line of code with the following:

<?php

$catquery = get_query_var('cat');

if (have_posts()) :

	if ( is_category() ) {

		foreach (get_the_category() as $category) : ?>
			
			<?php if ( $category->cat_ID == $catquery ) { ?>
				<div>
					<h2><?php echo $category->cat_name; ?></h2>
					<p><?php echo $category->category_description; ?></p>
				</div>
			<?php } // end of $catquery check
		endforeach;

	} // end of is_category check

	while (have_posts()) : the_post(); ?>

Let’s break it down to see what’s happening:

$catquery = get_query_var('cat'); // Which category was clicked

This interrogates the query string sent to WordPress when the site visitor clicks the category link in the menu, extracts the category ID from the query string, and stores it in a variable called $catquery.

if ( is_category() ) {

This conditional statement checks that we are in a category archive page and not a tags, author, or date archive page. This is more important if your archives are generated by archive.php, but is not needed if you use a category.php file. Having said that, I think it is always good practice to test a condition before generating output so as to reduce the possibility of errors or unexpected results in certain situations.

foreach (get_the_category() as $category) : ?>

This line foreach loop is where our get_the category() function creates an object named $category, which stores the member variables of all the categories assigned to the posts. The foreach PHP function means that such an object (ie an array) is created for each category assigned to the post.

<?php if ( $category->cat_ID == $catquery ) { ?>

This is where the magic happens. As the foreach loop runs we use a conditional check to see if the category ID in the $category matches the category ID stored in the $catquery variable. If it does match, we then display the category name and description in lines 12 and 13.

That’s it! We now have the category title and description displayed at the top of every category archive page. Don’t forget to add a category description in Dashboard > Posts > Categories!

Sub-categories and categories

Unfortunately, this method doesn’t work with sub-categories – only top level categories. A topic for a future article perhaps?

And finally…

Remember, this code is designed to work with category archives, the aim being to display the relevant category name and category description at the top of the archive page. There are probably further ideas which can be developed from this – which I haven’t had time to experiment with yet.

Related articles: 

Comments

  1. Martijn says:

    Funny, the descriptions at my site only show up in the subcategory pages; something I do wrong? I would like to show the descriptions of the parent categories too.

Leave a Reply to Martijn Cancel reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*


five − = 3