Advanced Wordpress - Category Based Navigation Instead of Page Based

April 22nd, 2008 - Posted in Development, Wordpress Tips |

Most Wordpress themes use pages for navigation. This is fine for most single user blogs, but when you want to really start expanding what Wordpress can do, look at using categories for your main navigation.

Both pages and categories are parent - child based, meaning you can create a top level category and then create categories under that main category. For an example of navigation with top level categories and sub categories, go to money.cnn.com/real-estate. Most media, or newspaper based websites use this format, and will really open up the possibilities of your blog.

I would not recommend starting with this format without any content. Make sure you have several posts. This type of blog needs a lot of content. They do not have to be well categorized because you can always change the categories. But if you are starting out and have no content, the first thing to do is organize your categories.

If you want to try out the code, just make a couple parent categories and then make a couple subcategories.

go to your theme folder and open header.php. comment out wp_list_pages(), and add the code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$defaults = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 0,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_image' => '',
'exclude' => '',
'hierarchical' => true,
'title_li' => '',
'echo' => 1,
'depth' => 1
);
 
wp_list_categories($defaults);

This will list only the top level categories. We do not want to display subcategories until we have clicked on a top level category. You do not need to list all defaults, but you do need to set depth to 1 in order to only show parent categories and set the title_li to nothing.

For more information about wp_list_categories click here

The next step is to display the subcategories when we have chosen a top level category. This is a little tricky because we have to get the category id for the parent category from the first post in that category.

directly after the main navigation make a subnavigation and add this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$cat = get_the_category();
$id = $cat[0]->parent;
 
$defaults = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 0,
'use_desc_for_title' => 1,
'child_of' => $id,
'feed' => '',
'feed_image' => '',
'exclude' => '',
'hierarchical' => true,
'title_li' => '',
'echo' => 1,
'depth' => 1
);
 
if($parents[0]->parent && !is_home())
{
echo "
<ul>". wp_list_categories($defaults) ."</ul>
";
}

The above code first gets the category of the first post in the main category. Then gets the parent id. Then in defaults we set the child_of to the id of the parent. This will get us all children of the main category. We do not want to show any children categories on the home page and if there are no posts we do not want to show any children categories.

That is all that is needed to make a category based navigation. I find it give many more options for your blog. To really customize your blog, you can make separate php files for each category. There is a category hierarchy Wordpress looks for.

  1. category-id.php - id = category id (6)
  2. category.php - this will handle all categories
  3. archives.php - this is probably used the most
  4. index.php - last but not least


4 Responses to “Advanced Wordpress - Category Based Navigation Instead of Page Based”

  1. 1 JeffX

    Great idea Matt, as you said, especially for sites who already have copious content.
    What do the SEO oracles have to say?

  2. 2 Richard Brown

    Hi

    I can’t the code to work and I am wondering if I have too many childs of? If you look at the site you can see the cats below. Any ideas please?

    Thanks

    Rich

  3. 3 Don

    Please help! This is exactly what I need, yet I can’t seem to get this code to work. Obviously, I dont just copy and paste all this where I sued to have:

    <?php wp_list_cats(’show_count=1&title_li=Categories’); ?>

    So what do I do? How do I understand the above and write it into my code?

  4. 4 Jason

    Great post. I have been searching for a solution to the page organization problem with wordpress for a while.

Leave a Reply


Add to Technorati Favorites

Featured Sections