WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hide pages in nav if not logged in? (4 posts)

  1. speedyp
    Member
    Posted 1 year ago #

    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) {
                $sorted_menu_items[]=$item;
            }
        }
        return $sorted_menu_items;
    }
  2. 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?

  3. speedyp
    Member
    Posted 1 year ago #

    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
    Replace
    <?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

    With...

    <?php
    //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;?>
  4. speedyp
    Member
    Posted 1 year ago #

    Resolved - I'm on a roll today :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.