How To: Display WordPress Categories without Children
About a week ago I decided I was unhappy with the archives plugin I was using to display our archives, so I decided to build a custom archives page. This archives page is fairly basic in that it displays the categories and archives side-by-side, then all the posts below it. This way people can use Ctrl+F to quickly find posts they are looking for.
After getting the page built, I realized all of the “child” categories I use for the WordPress Hacks theme was causing some problems, so I decided to remove them from the display. After all, these posts can also be found in their parent category.
If you are displaying your categories somewhere and would like to remove the children, here is the code you’ll need (for WordPress 2.5+):
<?php wp_list_categories('title_li=&depth=1&show_count=1') ?>
The depth=1 is the important one for the purpose of this hack as it is what tells WordPress not to display more than your parent categories. By default, it is of course set to depth=0 (shows all categories). If you are wondering what other options you have, here is what the WordPress Codex lists for the depth parameter:
- 0 – All Categories and child Categories (Default).
- -1 – All Categories displayed in flat (no indent) form (overrides hierarchical).
- 1 – Show only top level Categories.
- n – Value of n (some number) specifies the depth (or level) to descend in displaying Categories.
How To: Build a Categories and Archives Drop-down Box
Over the past couple years I’ve really enjoyed monitoring trends in the blogosphere and one of the trends that has come up recently is blogger’s cleaning up their sidebars by adding drop-down boxes.
If you’d like to build drop-down boxes for your categories and archives on your WordPress blog, here is the code you need:
Archives Drop-down Code
<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=\"\"><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>
Categories Drop-down Code
<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?>
<noscript><input type="submit" value="View" /></noscript>
</form>
I think something like this can be a good idea if done with the right theme, but I have also seen it on a few sites where it didn’t look very good, so keep that in mind if you decide to move your categories and archives to a drop-down box!
To see other code snippets we’ve featured here over the past year, check out our WordPress Code page!
Reader Poll: Do You Use Categories or Tags?
Today while browsing through my daily feeds, I noticed a great post over at Performancing about getting your blog organized with categories and tags. This post got the creative juices flowing a bit and got me thinking about both categories and tags.
I make it a point to engage in discussions regularly with my fellow bloggers about a variety of topics I have an interest in, including blogging, affiliate marketing, internet real estate, etc. One thing I always like to find out from fellow WordPress users is whether or not they’re using the tag system that was introduced in WordPress 2.3. What I’ve found are some very diverse responses, ranging from them not understanding how tags work, to some people that actually prefer to just use tags instead of categories. Of course in the middle are the people that are attempting to use both.
I personally use categories on all of my sites, but only choose to use tags on a few of them. For the ones that I do use tags on, I make sure to use the Recommended Tags WordPress plugin so I don’t go overboard with the tags I use.
I’d love to get your take on this. Do you use categories and/or tags on your blog?
How To: Finding Category Numbers in WordPress 2.5
One of the disappointing changes in WordPress 2.5 was the removal of category ID numbers. I thought this was just a minor oversight, but since the 2.5.1 upgrade this problem still was not addressed.
If you are looking for a category number (this is most often needed for setting up the homepage of Magazine WordPress themes), here is a really easy way you can locate it until the WordPress team adds the category number display we all took for granted before:
1) Go to Manage > Categories.
2) Hover your mouse over the category that you need the ID of.
3) Look in the lower left corner of your web browser for the ID of that category.
Here is a screenshot example:
Hopefully WordPress will again display the category ID in one of the future updates, but thankfully this workaround is really easy.
WordPress Talk – June 15, 2008
Here are some great WordPress-related posts I’ve enjoyed over the past week:
- Track WordPress 2.6 Progress – Quick Online Tips gives some suggestions on how to keep up with the progress of WordPress 2.6.
- How to Create a Dynamic Sidebar – This is something we’ve covered on a couple different occasions here on this site, but I wanted to point out this post because I think Richard does a great job of explaining how to tell WordPress what to display on each page.
- Project M Finally Announced – Justin Tadlock finally announced the details of his Project M. It appears it is a theme club of sorts, but with a twist. Click over to get the details.
- Do It Yourself WordPress Theme – Hayes Potter has come up with an interesting idea by releasing a WordPress theme. Simply build your stylesheet and you are done.
- WordPress Tricks – Stylized Web offers some useful PHP code snippets for WordPress.
- Making Authors Template Page – Also from Justin Tadlock, this post does a great job detailing how to make a WordPress authors template page.
- Displaying Related Categories and Content in WordPress – Darren Hoyt shares the code he used to make this feature in his popular Mimbo Pro theme.


















