Add a Private Page to Your Navigation Menu

Posted on January 29th, 2011

By default, any pages that you classify as “Private” are not displayed in the menu generated by the wp_list_pages function. In general this is a good idea, but it might be helpful to include the link if the logged in user is able to read private pages. Fortunately, WordPress has a function that will enable us to do exactly that:

<ul>
<? php // add a private page to your navigation menu
wp_list_pages('depth=1&title_li=0&sort_column=menu_order');
if(current_user_can('read_private_pages')) : ?>
 <li><a href="<?php echo get_permalink(10); ?>">For Authors only</a></li>
<?php endif; ?>
</ul>

Place this code where you would normally include the wp_list_pages tag and enjoy private-page links displayed in your navigation menus only for those users who are logged in and privileged enough to see them. The private link will not be displayed for the “ordinary” folk.

Make sure you replace the number “10” in the middle line to match the ID of the private page you would like to include.