Adding A Private Page Into The Navigation

WordPress Themes

If you use pages which have the state private, they will not be shown in the navigation. Nevertheless it can make sense to show this specific page in the navigation, if you want to provide easy access to this page for your users. The page should only appear to privileged users for sure. I will show you a little hack, which will let you exactly do this.

The Code

<ul>
<?php
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>

You have to copy this snippet to your theme at the place where the navigation is.
The function current_user_can checks, if the current user is privileged to read private pages. If not, then the page will not be shown for him.
Short notice: The 10 at get_permalink(10) is my ID for the page and has to be changed to your scenario.

Comments are closed.

25 comments

  1. Stefan Vervoort

    Thanks for this. Will use this in my upcoming theme!

  2. Alex

    Hey Stefan, glad to hear that some of our code will be included in your great website. 🙂

    Keep up the good work!

  3. Dawn

    Just implemented this in my design, worked perfectly, thanks for the hack!

  4. Alex

    Hey Dawn, glad it's working for you!

  5. Dave

    Thanks for this, its exactly what I was looking for! Now to update my theme.

  6. Tim Nash

    I'm using Atahualpa Theme by BytesForAll on WordPress. My pages and posts menus are across the top. I think that is defined in header.php but I don't know where abouts I should insert your code above to ensure that Private pages show up in the menu for users who are logged in. Can you help?

    Thanks!

  7. Michael

    Tim: I have no clue, how your theme is build. I think, the menu is in your header.php. look for a div id="menu2". Inside that div is a ul id="rmenu". After wp_listpages you can insert the code from

    if(current_user_can('read_private_pages')) :
    .....
    to endif ?>

    That should work.

  8. Josh Fialkoff

    Hi,
    I am trying to use this within the getcontent area of the loop, rather than on the sidebar, but I keep getting an error.
    Here is the code I have in my template. Can you please help me troubleshoot this?
    Thanks,
    Josh

    <a href="">For Authors only

  9. Michael

    sorry Josh, but the most of your code is missing.

  10. Josh Fialkoff

    Why yes it is! 😉
    Here's the full code.
    -Josh

    <a href="">For Authors only

  11. Josh Fialkoff

    The code does not seem to be coming through on my replies.

    Here is a link to the page code:

    http://dl.getdropbox.com/u/194649/code.rtf

    -Josh

  12. Michael

    Josh, the loop wasn't closed correcly.

    <?php
    	/*
    	 Template Name: Client Work Index
    	 */
    	?>
    <?php get_header(); ?>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<h2><?php the_title(); ?></h2>
    	<?php the_content(''); ?>
    <?php endwhlie; endif; ?>
    	<ul>
            <?php
              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); ? rel="nofollow">">For Authors only</a></li>
            </ul>
    <?php get_footer(); ?>
    

    And remember: the ID in get_permalink(10) must be replaced with the ID of your private page.

    Edit: fuck, the stupid editor adds a rel="nofollow" to the line with the link. Replace the line with yours.

  13. Josh Fialkoff

    Thanks Michael!

    I used your code and replaced the post number with a test page I have set up (I plan to use the current page call in the final version).

    I am getting a syntax error now on the final line:

    Parse error: syntax error, unexpected $end in /data/14/1/99/60/1262060/user/1346477/htdocs/Wordpress/wp-content/themes/BCR/client_index.php on line 18

    I thought that this error was the result of having extra spaces after the final close tag, but I don't have any spaces here, so I'm not sure what the problem is.

    Thanks again,
    Josh

  14. Josh Fialkoff

    BTW, the updated code is here: http://dl.getdropbox.com/u/194649/code.rtf

    Thanks,
    Josh

  15. Monky

    Hi, how can I add a children page of the private page?

  16. Q

    I posted the code above in my header and and change the (10) value to my new page. while login i see the page but once i log out it is still there.

    http://256studio.com/members is the page ..

    Q

  17. Q

    ok i sorta fixed it i used pagemash to hid the link. once i log out it doesnt show... but once i log in.. it is there...
    Now i can post private events to my members.

    Q

  18. Maria

    If i am not using a top menu but instead i am using a Widget (Pages) as my sidebar menu where do i have to insert the code.

    Thanks
    Maria

  19. Michael

    Maria, sorry but the code doesn't work with the page widget.

  20. enad

    is it possible to hide private pages from the visitors and show it only to logged in users/subscribers. been looking for a plugin for ths. but only a plugin which is applicable for posts.

  21. Suzanne Bissonnette

    Yes... I use this plugin:

    • Members Only Menu Plugin

    http://wordpress.org/extend/plugins/members-only-menu-plugin/

    Works fine, just one click to hide the page if not a connected user.

    Have fun !

4 pingbacks

  1. Interesting Articles #15 » DivitoDesign
  2. How to: Add privates pages in navigation menus
  3. How To: Adding Private Pages to Your Blog’s Navigation
  4. Interesting Articles #15 | SulVision