Have you ever felt a need to alter one of WordPress’s default page listings so that it doesn’t show up certain pages? I did when I was first starting out with WordPress, and for the life of me I couldn’t figure out a satisfactory way of doing it (sure I could have hard-coded the links into my template, but that’s not what I consider a satisfactory solution).
In fact, at the time, I wasn’t even sure if I was supposed to be changing any of my theme’s template files at all, after hearing all those dire warnings not to hack the core. Now that I’m a bit more experienced, it’s obvious that most people use a theme as a starting point for their site, but then customize it to suit their particular needs and taste, and to give their site a unique look.
The specific problem I had was that my theme (K2) inserts a horizontal list of all public pages in the header – not a problem in itself, but I had a couple of pages (Terms of Service, etc) that I didn’t want included as part of the main menu; I prefer to have those kind of pages linked from the footer.
The wp_list_pages function
Eventually I tracked down the WordPress function that generates those listings: the versatile wp_list_pages
. The specific line in my header.php
file that needed changing was:
wp_list_pages('sort_column=menu_order&depth=1&title_li=');
and all I needed to do to exclude the specific pages I didn’t want displayed was to change it to:
wp_list_pages('sort_column=menu_order&depth=1&title_li=&exclude=8,15,16,17,18');
where the numbers in the exclude
list represent the id’s of the pages I wanted hidden.
You can also do it the other way by specifying the list of pages to include
, which may make sense if you have a lot of pages, but only want to display a few of them. And there are lots of other ways to customize the display too, but I’ll simply direct you to the WordPress codex rather than trying to explain them all here.
Related posts:
Nice tip! I’ve been wondering how to do that on one of my sites because I want to add a few more pages but provide my own links to them in the sidebars only.
Thanks….
Dale
Thanks for your submission to the seventeenth edition of the Blog Carnival: Blogging. Your post has been accepted and its live: http://thatsblog.com/?p=119
-ThatsBlog.com
Many many thanks – after about 10 minutes messing around on WP.org, I gave up and came to Google to start searching.. Found your post, made the change, worked like a charm.
Glad to help
nice hack..thanks for posting.
This is by far the best explanation for this, well done
I do have a weird issue with it, I set it up and it worked perfectly
fine … I then decided to add another page to the site and hide it like the other 2 pages I was hiding, but this new page even with a new page # showed up.
pab
pab: do you mean the new page shows up even though you included its id in the exclude list? That’s never happened to me. Two things I would check:
– Have you got the right id for the new page?
– Is your syntax correct for the exclude list (comma-seperated, no spaces, etc)?
Thanks for replying Rodney
I’ve tried giving those page other numbers like 14 and 17 just any numbers to see if that could fix it
7 and 8 don’t show up the other 2 do.
once again thanks for taking the time really appreciate it
pab
I stopped worrying about doing this a while back. These days I just hard code in my menus. It doesn’t get changed that often, so its not biggy. Plus it’s one less php call.
Justin: True enough. Like I said though, I was looking for the WordPress way to do it.
Thanks for stopping by.
thanks.. was gettin errors when i tried to exclude one page.
Hiding some pages is very useful for Affiliate Marketers.
I use a WP plugin for hiding some of my pages.
Silki: what plugin is that?
Hi Rodney,
I have been using WordPress Hide Pages. It is available here http://www.instinct.co.nz/hide-pages-plugin/.
Although it states that it is comaptible With WP 2.5, but I am using it with WP 2.7.1 without any issues.
Thanks.
Silki
I just added the page ID in “exclude” part of the pages within the widgets and it worked fine