Silver Coins and Football Cards … For Sale
Peek A Boo Kitten with Neon Glow Effect
Toppled Euclyptus Tree
Sunday Recap – NFL Football Week 7
Buddy And The Purple Teddy Bear
Sunday Recap – NFL Football Week 6
Peek A Boo Kitten with Colored Pencil Effect
Basket Full Of Kitten Love
Become A Beta Tester For WordPress 2.9
Sharing Dinner: Petey (possum) and Rudy (cat)




How To Hide WordPress Subcategories From Display

September 23, 2009, Posted by Christopher at 2:43 am

WordPress Erase Image
This article will deal with how to hide certain categories and subcategories from displaying in your navigation menus. We have covered a couple of different ways to display WordPress categories and subcategories, but sometimes excluding some links can help to present even cleaner navigational menus.


Why do we need this ?
There are many different reasons that we may not want to display all of our categories all the time. For this article we are going to look at the “Photography Category” (link opens in new window) on this blog.

I wanted the photographs grouped upon the type of image, for example flowers, the ocean, sunsets, and the like through the use of subcategories. While that was going to be the main focus of this category, I also wanted the ability to create smaller photo albums based upon WHERE the picture was taken. Only about 30% of the photos would actually be placed in a geographic location sub-subcategory. I wanted this additional sorting ability so that I could write posts about the travel destinations portrayed in the photos and insert these photo albums into the posts.

See this graphical display:


WordPress Subcategory structure


The problem I ran into when I set this up is that all of the sub-subcategories under the geographic locations subcategory were displaying in the navigation menu on the parent category and subcategory pages. Obviously this was not needed. This was going to create crowded and messy navigation menus on down the line. I wanted to exclude the sub-subcategories from all displays except for inside the actual “Geographic Locations” subcategory.


How To Accomplish This
You might want to quickly read the article “How To Display Subcategories” to familiarize yourself with the “list categories” template tag that we are using to display the subcategory navigation menus.

The original template tag looked like this:

<?php wp_list_categories(’child_of=5&style=list&title_li=’); ?>

When you visit the “Photography Category” that template tag displays the navigation menu of subcategories. The menu appears on the page directly below the page header image.

The following changes were made:

<?php wp_list_categories(’child_of=5&depth=1&exclude=111&style=list&title_li=’); ?>

You will obviously need to change the number values above to match the category ID numbers of your specific blog, but the above tag can be broken down as follows:

  • Child_of=. This parameter tells WordPress to display all of the subcategories under the photography parent category. If this was the only parameter, then all of the subcategories and sub-subcategories shown in the image above would be displayed in the menu.
  • Depth=. This parameter tells WordPress to display ONLY the subcategories directly under the photography category. Sub-subcategories and all deeper category depths will not be displayed in the menu. So all of the actual location sub-subcategories such as San Fransisco would be excluded. Different values can be entered here to regulate how many depths you would like to display. You can visit the WordPress site at the link below for more information on this.
  • Exclude=. This parameter tells WordPress to NOT display the link to the “Geographic Locations” subcategory. This was a personal preference of mine and is not required. I excluded this category from the navigation menu as I wanted to hard code this link to the bottom of the navigation menu as opposed to having it appear in alphabetical order.
  • style=list&title_li=. These are display property parameters that will tell WordPress how to display the list. You can learn more about these on the WordPress Codex site here. (opens in new window)


From here, all I needed to do was set-up a template file for my “Geographic Locations” subcategory ( How To Do This ) and add a special navigation menu that would display the different location sub-subcategories. The template tag used to accomplish this goal was:

<?php wp_list_categories(’child_of=111&depth=1&style=&title_li=’); ?>


In Conclusion
What we did above was completely hide all references to the “Geographic Locations” sub-sub categories from ALL areas of this blog except for when you are actually in the geographic locations subcategory. You are presented with links to the “Patrick’s Point”, “Sierra Nevada Mountains”, and “San Francisco” photo album categories only after you have clicked on the “Photos By Geographic Locations” link.

This blog is not a “travel destination” blog so most of the visitors are probably just here to look at a certain type of photograph, ie: flowers, and are not overly concerned with WHERE the photo was taken. I don’t need to overwhelm the visitor with unwanted links to 100 different geographic locations. Instead I present them with one link to a page where they can then sort photos based upon location if they desire.

I think that you will find your navigational menus much cleaner and easier to navigate by removing some of the unneeded links. Take a look and see if you can’t hold off on displaying certain links until the visitor reaches a certain location or depth of your blog.


For more information on the “list categories” template tag and the different parameters that you can set, visit the WordPress Codex Site.


Hopefully this article has been a help to those of us who are not “code” addicts. I like to pass-along tidbits of information that might allow non-programmers the ability to modify their own websites. Please keep in mind that I am not a professional programmer but I have been building and modifying websites since 1995 using HTML, cgi, perl, MIVA, PHP, and CSS. Suggestions on other ways to reach the desired goals above are always welcome … please consider leaving a comment !

Always remember to save copies of your original files BEFORE you modify them. This will allow you to easily revert your web site if the changes do not work.

Comments ( leave a comment here ) and thoughts ALWAYS welcome !







Related Posts:




Technology and Programming categories:

>>> See All Technology Articles <<<



Related Tags:





Currently have 1 Comment

  1. Geoserv says:

    Excellent post, wasn’t sure how to do this. Thanks for the share.

Leave a Reply