Support » Networking WordPress » Hide pages in nav if not logged in?

  • Resolved speedyp


    Been searching for over an hour for a solution to this & ended up back here.

    I’ve created a special admin page for logged-in users. I’d like this page to be available / visible in the standard wp horiz menu, but only if you are logged in.

    I’ve tried to creating 2 different custom menus & switching between them with this, but it’s not working. Any ideas or alternative suggestions most welcome – Thanks

    add_filter('wp_nav_menu_objects', 'members_menu', 10, 2);
    function members_menu($sorted_menu_items, $args)
        if ($args->theme_location == 'Primary' && is_user_logged_in()){
            $header_members_items = wp_get_nav_menu_items('Header_members');
            _wp_menu_item_classes_by_context( $header_members_items );
            foreach ($header_members_items as $item) {
        return $sorted_menu_items;
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    if ( ($args->theme_location == 'Primary' )&& is_user_logged_in()){

    Maybe? I don’t know if you need to be more exclusionary. Though have you tested with just the is_user_logged_in part alone? To sort out which one of the ifs is killing it?

    Thanks Mika – just found another method that seems to work. It adds a “members only” nav menu option to the wp menus page 🙂

    I’ll show it here just incase anyone wants to do the same:

    Add to functions.php

    register_nav_menus( array(
    		'primary' => __( 'Primary Navigation', 'twentyten' ),
    		'members' => __( 'Members Only Navigation', 'twentyten' ),
    	) );

    In header.php
    <?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>


    //if the user is logged in, members only menu appears, if not, global menu appears
    if ( is_user_logged_in() ) :?>
    <?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'members' ) ); ?>
    <?php else :?>
    <?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
    <?php endif;?>

    Resolved – I’m on a roll today 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hide pages in nav if not logged in?’ is closed to new replies.