How to exclude pages from WordPress’s page listings

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:

  1. WordPress Translation plugin

19 Responses to “How to exclude pages from WordPress’s page listings”


  1. Gravatar of Dale 1 Dale

    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

  2. Gravatar of ThatsBlog 2 ThatsBlog

    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

  3. Gravatar of Elijah 3 Elijah

    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. :)

  4. Gravatar of Rodney Smith 4 Rodney Smith

    Glad to help :-)

  5. Gravatar of james 5 james

    nice hack..thanks for posting.

  6. Gravatar of pab 6 pab

    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

  7. Gravatar of Rodney Smith 7 Rodney Smith

    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)?

  8. Gravatar of pab 8 pab

    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

  9. Gravatar of Justin 9 Justin

    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.

  10. Gravatar of Rodney Smith 10 Rodney Smith

    Justin: True enough. Like I said though, I was looking for the WordPress way to do it.
    Thanks for stopping by.

  11. Gravatar of mobilex 11 mobilex

    thanks.. was gettin errors when i tried to exclude one page.

  12. Gravatar of Silki @ Internet Security Blog 12 Silki @ Internet Security Blog

    Hiding some pages is very useful for Affiliate Marketers.
    I use a WP plugin for hiding some of my pages.

  13. Gravatar of Rodney Smith 13 Rodney Smith

    Silki: what plugin is that?

  14. Gravatar of Internet Security Blog 14 Internet Security Blog

    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

  15. Gravatar of SkippyJ 15 SkippyJ

    I just added the page ID in “exclude” part of the pages within the widgets and it worked fine :)

  1. 1 links for 2008-08-19 [delicious.com] | afewgoodpens.com
  2. 2 ThatsBlog.com » Blog Archive » Blog Carnival: Blogging: Seventeenth Edition
  3. 3 carnival of struggling bumbling newbies – August 30, 2008 : SuccessPart2.Com
  4. 4 August 2008 Edition Of Wordpress Posts – Wordpress Posts
Comments are currently closed.