Advanced Wordpress - Category Based Navigation Instead of Page Based
April 22nd, 2008 - Posted in Development, Wordpress Tips | 4 Comments »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.
- category-id.php - id = category id (6)
- category.php - this will handle all categories
- archives.php - this is probably used the most
- index.php - last but not least
Related Posts:
|
Posted in:
Development Whether you are a programmer of executive, This is where you find the technical aspects of web based application |







Recent Comments