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:
and all I needed to do to exclude the specific pages I didn’t want displayed was to change it to:
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.