Front-end Web Developer | Tampa, FL

How to include or exclude pages from header menu in WordPress

Posted on: February 28th, 2010 by admin No Comments

 

How to customize WordPress header navbar

 

The code to add to the header.php file to include only pages 43 and page 44 will look like the examples below. You can add as many page numbers as you want in a comma separated list, or just one.

{CODE type:php;}
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&include=43,44’); ?>
{/CODE}

 

Look closely at the difference between the first example and the added code in the second. The code added was this &exclude=43,44 with no extra spaces and keeping the ‘ at the end.

 

To exclude certain pages from appearing in the navigation bar simply use exclude instead in include in the code. The example below will not show pages 43 and 44:

{CODE type:php;}
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&exclude=43,44’); ?>
{/CODE}

 

Now what about sorting the order the pages are shown? Well there are options that you can use and these are found on the WordPress codex. But if none of those options work for you you could use the includes code multiple times to set the order on your own. For example this would show page number 43 first then page 1 second:

{CODE type:php;}
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&include=43’); ?>
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&include=1’); ?>
{/CODE}

 

Did you notice the code for the Home link in the first code example? I bet you are thinking now right? Yes that is the code we can learn from to hard-code external links. This part is very simple just place the code for the external link anywhere within the navigation bar you want the link to appear. In the sample below the Home link is first, page number 43 second, an external hard-coded link and last is page number 1.

{CODE type:php;}
<ul id=”nav”>
<li><a href=”<?php echo get_settings(‘home’); ?>”>Home</a></li>
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&include=43’); ?>
<li><a href=”http://www.sopov.com/” title=”Sopov.com”>PSD to HTML</a></li>
<?php wp_list_pages(‘title_li=&depth=2&sort_column=menu_order&include=1’); ?>
</ul>
{/CODE}

 

Special thanks wordpressmax.com for this article. I hope it will be useful.

Comments are closed.