Skip to main content
added 1 character in body
Source Link

Based on @jamio answer I agree that if you just display:none the menu isits still accessible thru inspect element, therefore I come up with the solution that if you want to hide the nav menus based on their class you can use classes[0] instead of title so that you can now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer I agree that if you just display:none the menu is still accessible thru inspect element, therefore I come up with the solution that if you want to hide the nav menus based on their class you can use classes[0] instead of title so that you can now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer I agree that if you just display:none the menu its still accessible thru inspect element, therefore I come up with the solution that if you want to hide the nav menus based on their class you can use classes[0] instead of title so that you can now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}
added 136 characters in body
Source Link

Based on @jamio answer I agree that if you just display:none the menu is still accessible thru inspect element, therefore I come up with the solution that if you want to hide the nav menus based on their class you can use classes[0] instead of title so that can you can now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer if you want to hide the nav menus based on their class you can use classes[0] instead of title so that can you now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer I agree that if you just display:none the menu is still accessible thru inspect element, therefore I come up with the solution that if you want to hide the nav menus based on their class you can use classes[0] instead of title so that you can now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}
added 18 characters in body
Source Link

Based on @jamio answer if you want to hide the nav menus based on their class you can use classes[0] instead of title so that can you now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove "Downloads" menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer if you want to hide the nav menus based on their class you can use classes[0] instead of title so that can you now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove "Downloads" menu item when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}

Based on @jamio answer if you want to hide the nav menus based on their class you can use classes[0] instead of title so that can you now just add a class on menu instead on setting each menu thru code

 add_filter( 'wp_nav_menu_objects', 'remove_menu_items', 10, 2 );
function remove_menu_items( $items, $args ) {

    $logged_in = is_user_logged_in();

    foreach ( $items as $item_index => $item ) {
        // Remove menu item with "hide_when_logout" class when not logged in
        if ( $item->classes[0] == 'hide_when_logout' ) {
            if ( !$logged_in ) {
                unset($items[$item_index]);
            }
        }
    }
    return $items;
}
Source Link
Loading