What the Hell are Darkly Glasses?

Realizing my category names are oftentimes a bit cryptic, I have been seeking a way to ensure my readers were able to decipher them.  Here is my version of how to add category descriptions to WordPress (category) archive pages. I worked this out with the help of another WP user over at their forums. I also made use of this page at WP.

Basically, what it comes down to is adding one line of code to your post loop. Initially, I thought a plugin was required, but this method requires no additional plugin—merely the addition of one line of code (in the aforementioned loop).

I decided that the best location for this category description adding code was in my archive.php file. I use the deafult theme (slightly modified) and your theme may or may not have an archive.php file. You will have to work out for yourself where to place this line of code. If you are, like me, using the default theme, I recommend making a copy of the default theme (I called mine defaultMod) so that you can always refer back to the original default theme and its code.

In your archive.php file you will find the following line of code:

<h2 class=”pagetitle”>Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>

Following that, I added a new line of code to include the appropriate description:

<?php
$category = get_the_category();
echo category_description();
?>

I wanted to make some other wiggles, and so my final product actually looks like this:

<!--  Default category title code  -->
<!--		<h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>  -->

<!--  Modified category title and added description code  -->

<h2 class="pagetitle"> <?php single_cat_title(); ?> </h2>
<center>
<?php
$category = get_the_category();
echo category_description();
?>
</center>
<br />

<!--  Back to default  -->

As you can see, I like my modifications to be well documented. I also chose to center my description.  (I should probably put that centering into a css but I haven’t.)

Once you have made this change, your category descriptions should appear on any category archive page; they will appear above the first post. I have tested this against posts with multiple categories and against cases where the description is empty and against categories and sub-categories. It seems to be working as expected all around.

Share

Leave a Reply

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