How To Hide A Page Or Category Link In WordPress
When it comes to customizing a WordPress theme to your liking you are provided with various different options. If one of those things requires excluding a page or category link from showing in your navigation you have come to the right place. One of the reasons may be that you have a thank you page setup and is only meant to be seen after a visitor subscribes to your newsletter or various other situations can call for hiding a page/category link.
In order to get started we need to find which file the code for the navigation that you are wanting to customize is located. With most WordPress themes, a horizontal navigation bar located near or at the top of the page will be placed in the header.php file. Otherwise you will most likely be editing a file called sidebar.php, of course depending on your theme it may be entirely different.
Finding the category IDs
Before we get into the actual code and editing it, I want to recommend that you first determine the page or category id(s) that you are wanting to exclude from showing in a list. We are going to need the id(s) to add into the code so having them ready beforehand can make this go a little bit quicker. To determine the id of the page or category you need to get the list of your pages/categories. If you are using WordPress 2.7, the categories list can be found under the Posts section and the pages under the Pages section.
Once you can see the list of your pages/categories all that you have to do to find the id is to mouse over the title of each one and look in the status bar of your web browser. If you are looking at your categories you should see a number listed after &cat_ID= in the status bar. This number is the id of the category that you are currently hovering over. A page’s id is slightly different and will be shown after &post= in the status bar.
Now that you know how to find the category and page ids, make a note of which ones you want to exclude from showing.
Editing the code itself
The next step is to edit the actual code so you need to either connect to your web server through FTP, a file manager or you may also use the Theme Editor inside of the WordPress Dashboard which can be found under the Appearance section labeled as Editor in 2.7. Once you have either header.php or sidebar.php open you need to search for a line of code depending whether you are wanting to hide a page link or a category link.
When looking for the code that displays pages you want to find
<?php wp_list_pages(); ?>
Or for the category code
<?php wp_list_categories(); >
As a quick note you may or may not see extra text within the () in the code. Whether or not you do see it does not matter as we are going to be inserting code in between the () anyways. We are going to be using &exclude= followed by the id of the page or category in order to prevent it from showing.
For example, lets say that the page you are wanting to hide has an id of 5. In order to hide it you would need the code to look like
<?php wp_list_pages('&exclude=5'); ?>
Or if the category you were wanting to hide had an id of 32 the code would look like
<?php wp_list_categories('&exclude=32'); ?>
Simple enough? As you can see, the id of the page/category is listed after &exclude= which tells WordPress not to show the link.
Whats that? You want to hide multiple pages or categories you say? That’s easy!
All that you need to do is place a comma after the first id followed by the id of the next page or category.
<?php wp_list_pages('&exclude=5,25'); ?>
- Hides pages with ids of 5 and 25
<?php wp_list_categories('&exclude=10,43,85'); ?>
- Hides categories with ids of 10, 43 and 85
If you find that there is other code listed inside of the () make sure that when adding in the code that you place an & in front of exclude= in order to prevent an error.
There you have it, how to hide a page or category link from showing in a list. You may have gotten lucky and found that your theme was already using exclude and you just had to add in extra ids. If not, you can still see how easy it was anyways!
If you have any questions or can’t get this to work on your WordPress theme feel free to leave a comment below :)
Feedback!
If you read this post and understood it great! Let me know in the comments below. If you are still having issues and can't get it figured out, you are more than welcome to leave a comment or send me an email using the contact form and I'll do what I can to help!
Wow! Thank you!
I always wanted to write in my blog something like that. Can I take part of your post to my site?
Of course, I will add backlink?
Regards, Timur I. Alhimenkov
Hello!
Really nice tutorial!! I wanted to hide a categorie…I have all what i need!
When i try to add “&exclude=32″,
I have an error:
Parse error: syntax error, unexpected T_VARIABLE in /var/www/sites/…/sidebar.php on line 1
Maybe because of Php5?
Thanks for your help!!;-)
Gaetan
Hi Gaetan, it sounds as though something else is the issue. It is saying you have an unexpected variable listed on line 1 in your sidebar.php file and I am going to assume that something like
wp_list_categories();isn’t being used on the first line of your sidebar.php file.Plus, I have not heard anything of this not working on PHP5 and out of all my sites that use PHP5 it works. Would you be able to post the first few lines of your sidebar.php file here so maybe I could try to figure out what is going on?
This could really help me, thank you! But I have a question:
What about permalinks? The ID of my posts are not numbers, they’re the post’s tile with hyphens because I’m using permalinks.
Hi Lillea, thanks for your question.
It will work just fine with any kind of permalink whether you are just showing the id of the post, the date and the title, or just the title as you explained.
You can find the ID of each post by visiting the Edit Posts page found under the Edit link in the Posts section of your Admin Dashboard and by hovering your mouse over the title of the post. Then look in your status bar located at the bottom of your browser and make a note of the number at the end of the url. This number will be the ID of that post.
Let me know if you need anymore help or further clarification!
Hi again,
I meant ‘post’s title’
I tried to follow your directions with the theme I’m using because the categories have numbers at least, but I cannot locate the code you mention in either the header.php or sidebar.php
The theme Im using is Mandigo (not sure if you’re familiar with it) and the closest I can find to the code you mention in sidebar.php is
‘name’,
‘optioncount’ => 1,
‘hide_empty’ => 0,
‘hierarchical’ => 1,
)
I would love to be able to hide a category (the posts in the category will be password protected too ideally)
Ah ok, I have never used the theme before but just downloaded it right now and took a look at the code.
In the sidebar.php file where you found the code you showed above you are going to slightly modify it in order to get it to display correctly and make it easier for you to exclude categories. (Not to mention they are using an old piece of code to show the categories instead of the newer code).
So normally the code looks like
wp_list_cats(
array(
’sort_column’ => ‘name’,
‘optioncount’ => 1,
‘hide_empty’ => 0,
‘hierarchical’ => 1,
)
);
and you are going to want to change it over to the following code
wp_list_categories(’sort_column=name&optioncount=1&hide_empty=0&hierarchical=1&exclude=3‘);
It looks a little bit different but it will still produce the same results. Notice that I added in the exclude parameter in order to hide the category with the ID of 3. All that you have to do is find the ID of your category and replace it with the number that is bolded above.
If you want to hide multiple categories just add a comma after the 3 with the other ID(s) of the categories.
Example: &exclude=3,4,105
The above would hide the categories with the IDs of 3,4 and 105.
Thank you! I hadn’t realised that there was no ’subscribe to comments’ here so I didn’t see your reply until now.
That helps a lot! :)
Actually, I sorted it myself like this:
Thank you very much for this guide! :)
Hi Jarret
The only place I can find any code for the listing of pages is in the them functions file (functions.php). The code looks like this:
{
global $artThemeSettings;
if (true === $artThemeSettings['menu.showHome'] && ‘page’ != get_option(’show_on_front’))
echo ‘‘.$artThemeSettings['menu.topItemBegin']
. $artThemeSettings['menu.homeCaption'] . $artThemeSettings['menu.topItemEnd'] . ‘‘;
add_action(’get_pages’, ‘art_header_page_list_filter’);
add_action(’wp_list_pages’, ‘art_list_pages_filter’);
wp_list_pages(’title_li=’);
remove_action(’wp_list_pages’, ‘art_list_pages_filter’);
remove_action(’get_pages’, ‘art_header_page_list_filter’);
}
Should I be putting the &exclude parameter somewhere in here?
Regards
John
Hi Jarret
Actually, I found a plugin called Exclude Pages that worked like a charm.
Thanks
John