Did you know that you can change the sort order of posts and categories listed in menu items with WordPress, and can even eliminate some items that you might not want to display? WordPress uses built-in PHP functions and the output is easily modified without the need to alter the code in the functions themselves.
There is a good reason for not altering the code in the core WordPress scripts unless it is absolutely necessary to do so. Most PHP functions are part of the core code, and if you change the code in these scripts, you will lose your changes the next time you upgrade WordPress.
WordPress developers have set up the system so that you can alter the behavior of several PHP core functions by inserting arguments sent with the call to the function from the theme template scripts. Arguments are name-value pairs, which are sometimes called parameters. You must use a valid name for an argument and assign a valid value in order for a change to work.
There are two functions to look for in your WordPress theme. In most themes, you will find the calls to the menu listings for pages and categories in the sidebar.php script. Sometimes, a designer will place the call to the pages function in the header.php or footer.php script. If you are using sidebar widgets, this information does not apply, because widget code calls to the functions will be found in other scripts.
The two function to look for are the following:
There may already be a set if arguments added to the function call. Be carefully with what you add or remove, because you could break the display of the menu items. Separate arguments are delineated by the ampersand sign ( & ).
Here is a typical set of arguments included in the call to the wp_list_categories function:
These arguments turn on the category post counts and display the name Categories above the list.
If you have an old template, you may see calls to functions named wp_list_cats or list_cats. These functions have been depreciated since WordPress 2.1 and should be replaced with the current version, which is wp_list_categories.
Both the wp_list_categories and wp_list_pages functions allow you to use arguments within the parentheses that alter the way the way the functions work.
How to Disable the Post Counts in Categories
WordPress category menu with post counts” />Some themes are set up with the post count feature turned on, while other have it turned off. It is turned off by default, which means that if you want to disable post counts, all you have to do is remove the argument. If it is the first argument in the set, make sure that the trailing ampersand that separates the arguments is also removed. If you like post counts and want to turn them on, simply add the argument to the function call.
To turn off the post count, remove the argument show_count=1 and any ampersand separating it from another argument. As an alternate, you could change the value to zero ( 0 ), which would have the same effect. If you want to turn the post count on, change the value to 1. If the show_count argument is missing, add the following as the first argument: show_count=1&. Do not forget to add the ampersand to separate arguments.
Changing the Title for a Category or a Page List
This one is easy and the same argument works for both functions. If the title argument is already included, just change the name. The name for the title argument is title_li.
Be careful that you do not alter any HTML code surrounding the title. To change the title from “Categories” to “Sections”, just change the word or words that you wish to alter.
Including or Excluding Items from a List
You will need to know the category or page IDs for the items that you wish to include or exclude from a list. You will find these in the Admin area of WordPress. Select a category to edit and the IDs can be found in the URL in your browser’s address bar. The category ID will be designated by cat_ID=2 at the end of the URL. Pages use post IDs, so they will be designated as post=6.
The argument to add to only include a few categories or pages is include=2,4,10, where the only categories that you wish to display are those with IDs 2, 4 and 10.
The argument to exclude certain categories or pages is exclude=2,4,10, where the only categories that you wish to exclude from the category list are those with IDs 2, 4 and 10.
Category and page exceptions are an either-or proposition, so do not try to use both at the same time.
San Diego Web Design says
Thank you, very helpful. I was looking for a way to tweak my top menu without having to install a plug-in. There don’t seem to be any easy to use plug-ins out there. I tried Suckerfish and Nav, but I think they are too cumbersome to use. Your explanation helped me accomplish what I needed to do. Thanks.
Elizabeth Day says
I came across your site while searching for an answer for my category problem. I copied and saved your advice for future reference, I want to keep some categories from showing up. I can’t find the answer I need for a bigger problem, hope you can help.
My categories across the header and on the sidebar only list 1 article in the category even though there may be between 5 and 7 articles listed in that category. I have 5 categories and they all do it. I looked at permalinks, changed themes, disabled plugins and widgets, still have problems. I clicked on YOUR sidebar categories and was taken to a page that listed articles in each of your categories. I want what you have. Can you advise? Thanks,
Your problem does not appear to be related to the category menu code. It looks like the menus are taking a user to the category pages, but the category pages are not displaying all the posts in the category. The fact that you mentioned that the problem did not go away when you changed themes indicates that is it probably not a coding problem. It looks like you have the configuration set up in the WordPress to only display 1 post on the category pages.
Check: Settings > Reading > Blog pages show at most ___ posts.
I’ll bet this is set for 1 post. Try changing it to 10.
Let us know if that works.
Jesse Skeens says
Thanks for this, seems you have a typo though, for exclude you say “include=2,4,10” in the instructions, although it’s correct in the code example.
Thanks, Jesse. It is now corrected. 🙂
Thomas - Business Buzz says
Hmm how strange!
I had an issue where I couldn’t exclude a Category from my menu within a Widget, even with typing in the cat_ID.
I was searching for a solution and found this page.
Trying to figure out where I’m going to place the PHP code, I looked in the webpage source code.. and voila! It actually lists the Category page number in the source code!
cat_ID was 3, but page number was actually 18!
So this page indirectly solved my problem, thank you!