Hiding A Page Or Post Link In WordPress

An updated version of this post can be found at WPThemeSupport.comHow To Hide A Page Or Category Link In WordPress

There may come a time when you are adding a page or post to your blog that you may not necessarily want to show up in your main navigation or in a list of posts in your sidebar or wherever it may be. I have a page that people are redirected to after they sign up for advertising on my blog. It is just a simple Thank You page which thanks them for their purchase, gives them further information on how soon they should hear from me and I provide a list of messenger IM’s in case they need to get in touch with me.

I’m sure that there is a plugin out there that exists that does exactly what I am about to show you but it is quite simple to do and do you really need another plugin installed on your blog? Lets get started.

Make your way into your WordPress files through either FTP or your web hosts file manager. Find your blog’s current theme directory ( /wp-content/themes/nameoftheme ), if you are wanting to hide a page from showing up in your main navigation on your blog you will most likely want to open up header.php. Out of all of the themes that I have seen, header.php always contains the code for the main navigation links. If you are looking to change a list of posts contained within the sidebar of your blog you are going to want to open up a sidebar.php or similarly named file.

If we are dealing with pages you want to look for the following code

<?php wp_list_pages(); ?>

There very well may be code contained within the () and this is where we are going to be placing code anyways to hide a page. I’m going to take the code that I use for my blog and use it is an example.

<?php wp_list_pages('title_li=&exclude=115,38'); ?>

Notice the extra code inside of the parentheses. Normally, when you just use the first example without anything inside of the () WordPress will add the word Pages before your list of pages. The code title_li= removes the word Pages and instead just list each page. The second piece of code separated by the & sign is the code that hides a certain page. Exclude=115,381 hides the pages with ID’s of 115 and 381. Depending upon how you have your permalinks setup you may not be able to see the ID of a page or post.

Go into your WordPress administration panel, click on Manage, then click on Pages. Mouse over each of the page titles while looking in the status bar at the bottom of your screen. Take note of the number at the end of the text located after post=, this is the ID of the page. Then all that you have to do is simply plug that number into the above example code that I listed. If you are wanting to hide more then 1 page like I did, make sure to separate each number with a comma.

That is how we deal with hiding a page link in WordPress and I mentioned in the title about hiding a link to a post as well. This part can get a little bit tricky as there are different ways to go about listing posts on your blog. If in fact the list is in your sidebar of your blog, open up sidebar.php listed in your blog’s theme directory and look for wp_get_archives() or get_posts(). Either one of these has the potential to lists posts on your blog and it mainly depends upon how the theme designer set it up.

The code for excluding a post is still the same, exclude=. The number of the post goes after the equals sign and the post ID can be found under Manage, Posts inside your WordPress administration panel. You can find the post ID the same way we did with the page ID by mousing over each post and looking at the last number listed in the status bar of your browser. Remember to separate each post ID with a comma if you are excluding multiple posts from your list.

As a quick example, if the wp_get_archives tag was used, the code may look like

<?php wp_get_archives('type=postbypost&exclude=125,85'); ?>

The first variable type=postbypost does exactly what it says, list the archives post by post. The second variable exclude=125,85 is where we are excluding the posts by ID. You may find out that when looking at your code that exclude= is nowhere to be found. If you do add the exclude= code into the tag make sure you separate it and a previous variable with the & character.

<?php wp_get_archives('type=postbypost&exclude=125,85'); ?> WORKS
<?php wp_get_archives('type=postbypostexclude=125,85'); ?> DOESNT WORK

If you have any questions or get lost while trying to do this yourself, either leave me a comment here or send me an email and I’ll do what I can to get things working.

Comments - Leave a comment

Got something to say?