wp_get_nav_menus example

/** * Get the classic navigation menu to use as a fallback. * * @deprecated 6.3.0 Use WP_Navigation_Fallback::get_classic_menu_fallback() instead. * * @return object WP_Term The classic navigation. */
function block_core_navigation_get_classic_menu_fallback() {

    _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::get_classic_menu_fallback' );

    $classic_nav_menus = wp_get_nav_menus();

    // If menus exist.     if ( $classic_nav_menus && ! is_wp_error( $classic_nav_menus ) ) {
        // Handles simple use case where user has a classic menu and switches to a block theme.
        // Returns the menu assigned to location `primary`.         $locations = get_nav_menu_locations();
        if ( isset( $locations['primary'] ) ) {
            $primary_menu = wp_get_nav_menu_object( $locations['primary'] );
            if ( $primary_menu ) {
                return $primary_menu;
            }

    } else {
        $key = array_search( $nav_menu_selected_id$nav_menu_option['auto_add'], true );
        if ( false !== $key ) {
            unset( $nav_menu_option['auto_add'][ $key ] );
        }
    }

    // Remove non-existent/deleted menus.     $nav_menu_option['auto_add'] = array_intersect(
        $nav_menu_option['auto_add'],
        wp_get_nav_menus( array( 'fields' => 'ids' ) )
    );

    update_option( 'nav_menu_options', $nav_menu_option );

    wp_defer_term_counting( false );

    /** This action is documented in wp-includes/nav-menu.php */
    do_action( 'wp_update_nav_menu', $nav_menu_selected_id );

    $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
        sprintf(
            
echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
            echo "</wp:author>\n";
        }
    }

    /** * Outputs all navigation menu terms. * * @since 3.1.0 */
    function wxr_nav_menu_terms() {
        $nav_menus = wp_get_nav_menus();
        if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) {
            return;
        }

        foreach ( $nav_menus as $menu ) {
            echo "\t<wp:term>";
            echo '<wp:term_id>' . (int) $menu->term_id . '</wp:term_id>';
            echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
            echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
            wxr_term_name( $menu );
            echo "</wp:term>\n";
        }
$menu_locations     = array_merge( $menu_locations$new_menu_locations );
            // Set menu locations.             set_theme_mod( 'nav_menu_locations', $menu_locations );

            $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . __( 'Menu locations updated.' ) . '</p></div>';
        }

        break;
}

// Get all nav menus. $nav_menus  = wp_get_nav_menus();
$menu_count = count( $nav_menus );

// Are we on the add new screen? $add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false;

$locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false;

$page_count = wp_count_posts( 'page' );

/* * If we have one theme location, and zero menus, we take them right * into editing their first menu. */
$num_locations = count( get_registered_nav_menus() );

        if ( 1 === $num_locations ) {
            $locations_description = __( 'Your theme can display menus in one location.' );
        } else {
            /* translators: %s: Number of menu locations. */
            $locations_description = sprintf( _n( 'Your theme can display menus in %s location.', 'Your theme can display menus in %s locations.', $num_locations )number_format_i18n( $num_locations ) );
        }

        // Pass data to JS.         $settings = array(
            'allMenus'                 => wp_get_nav_menus(),
            'itemTypes'                => $this->available_item_types(),
            'l10n'                     => array(
                'untitled'               => _x( '(no label)', 'missing menu item navigation label' ),
                'unnamed'                => _x( '(unnamed)', 'Missing menu name.' ),
                'custom_label'           => __( 'Custom Link' ),
                'page_label'             => get_post_type_object( 'page' )->labels->singular_name,
                /* translators: %s: Menu location. */
                'menuLocation'           => _x( '(Currently set to: %s)', 'menu' ),
                'locationsTitle'         => 1 === $num_locations ? __( 'Menu Location' ) : __( 'Menu Locations' ),
                'locationsDescription'   => $locations_description,
                'menuNameLabel'          => __( 'Menu Name' ),
                

    public function form( $instance ) {
        global $wp_customize;
        $title    = isset( $instance['title'] ) ? $instance['title'] : '';
        $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';

        // Get menus.         $menus = wp_get_nav_menus();

        $empty_menus_style     = '';
        $not_empty_menus_style = '';
        if ( empty( $menus ) ) {
            $empty_menus_style = ' style="display:none" ';
        } else {
            $not_empty_menus_style = ' style="display:none" ';
        }

        $nav_menu_style = '';
        if ( ! $nav_menu ) {
            
return $classic_menu_fallback;
    }

    /** * Determines the most appropriate classic navigation menu to use as a fallback. * * @since 6.3.0 * * @return WP_Term|null The most appropriate classic navigation menu to use as a fallback. */
    private static function get_fallback_classic_menu() {
        $classic_nav_menus = wp_get_nav_menus();

        if ( ! $classic_nav_menus || is_wp_error( $classic_nav_menus ) ) {
            return null;
        }

        $nav_menu = static::get_nav_menu_at_primary_location();

        if ( $nav_menu ) {
            return $nav_menu;
        }

        
// Get the nav menu based on the requested menu.     $menu = wp_get_nav_menu_object( $args->menu );

    // Get the nav menu based on the theme_location.     $locations = get_nav_menu_locations();
    if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
        $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
    }

    // Get the first menu that has items if we still can't find a menu.     if ( ! $menu && ! $args->theme_location ) {
        $menus = wp_get_nav_menus();
        foreach ( $menus as $menu_maybe ) {
            $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) );
            if ( $menu_items ) {
                $menu = $menu_maybe;
                break;
            }
        }
    }

    if ( empty( $args->menu ) ) {
        $args->menu = $menu;
    }
Home | Imprint | This part of the site doesn't use cookies.