Advanced Category Usage
February 26, 2010 in Wordpress by Paul D'AmoraMost blogs out there use some sort of category system. They’re good for SEO, and for overall ease of use while surfing a website. As most of us know, WordPress supports a wonderful categorization system. There are …
Most blogs out there use some sort of category system. They’re good for SEO, and for overall ease of use while surfing a website. As most of us know, WordPress supports a wonderful categorization system. There are a lot of things that can be done with those categories that some people just don’t know. Let’s focus on the display of those categories. If your users can’t see your category in an informative and helpful way, then there is really no point in having them. This includes displaying the list of category that a post may be in, displaying category pages, and the display of lists of categories. Throughout this post I will cover advanced usage of all of these.
In WordPress you can easily list out the categories that a post was posted in. This is done with a single tag, that must be within the Loop.
<?php the_category(', '); ?>
You can change a few parameters of this tag, including the separator. This is the piece of text that separates each category if there’re more than one. In the code I gave you, it is a comma. You can change it to a bullet •, an arrow >, or whatever else suits you.
This piece of code will display links to each of the individual category pages. You can change the structure of these links on the Permalinks page in you WordPress admin settings.

For example, if you use articles as your base, then your category pages will be linked as such
http://example.com/articles/uncategorized
When WordPress is trying to display the category page, it is going to look for certain template files. If they aren’t found, then it looks for the next one. For category page, WordPress use the category.php file. You are welcome to write out your own category.php file, but I typically use the next template file, which is archive.php. For more info on what template files WordPress uses to render pages, visit DigWP.
Anyway, your archive or category page can typically be a modified version of the Loop used on the index page. If you want to leave out something, or add some type of content, then go ahead. One thing you may want to do is display a descriptive heading. This tells the user what they’re viewing exactly. I use the following
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class="archive">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h2 class="archive">Posts Tagged ‘<?php single_tag_title(); ?>’</h2>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2 class="archive">Archive for <?php the_time('F jS, Y'); ?></h2>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h2 class="archive">Archive for <?php the_time('F, Y'); ?></h2>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h2 class="archive">Archive for <?php the_time('Y'); ?></h2>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h2 class="archive">Author Archive</h2>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h2 class="archive">Blog Archives</h2>
<?php } ?>
This piece of code runs through several if and elseif statements to determine what type of page you’re viewing, and display a heading accordingly. You can use it in your own archives page by inserting the above code after
<?php if (have_posts()) : ?>
in your archive.php file.
Lastly, let’s cover listing out your blogs categories. This is easily done with the following code -
<?php wp_list_categories( ); ?>
This will list out all of the categories and their children that exist on your blog. You can do things to customize this output such as exclude categories, or change the title of the list
<?php wp_list_categories('exclude=1,2&title_li=New Title'); ?>
Or only show the parent categories.
wp_list_categories('depth=1'); ?>
The list goes on and on.
You may have noticed on your WordPress admin category page that you can add descriptions to the categories. Well, with a bit of php magic you can list out the names of the categories along with their descriptions.
First, go into your functions.php file and insert the following function
function advanced_cat () {
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'pad_counts' => '1'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '><span>' . $category->name.'</span> ';
echo '<span>'. $category->description . '</span></a></li>';
}
}
This is creating a foreach statement that defines the display for each category in the list. In this case we have it echoing the entire category name with its description inside an li tag. The category name is displayed within a link, and a span. The categories description is then outputted inside a span, as well as within the same link and list element that the category name was listed in. You can of course customize it and add classes, or more data.
In order to actually display our list you need to put the following code somewhere in your theme -
<?php advanced_cat(); ?>
This simply outputs the function we created earlier in the functions.php file.
Fin
Well that concludes today’s post. Thanks go out to comment author – Benje for bringing up the question on how to accomplish this.
Thank you for printing out this article. I'm glad you found the content on this site useful, and I hope you found everything you were looking for. For more awesome content like this, just visit http://net-cake.com, and contact me with any questions or concerns.
« Slice in the Works – Carpe Diem Recreating the Coda Popup Bubble With CSS »














Comments
Benje February 26th, 20105:51 pm
Thanks bro
I really appreciate it.
Paul D'Amora February 27th, 20101:43 pm
Glad I could help.
pharmacy technician March 10th, 20108:48 pm
What a great resource!
dental hygienist April 11th, 20102:43 am
My cousin recommended this blog and she was totally right keep up the fantastic work!