wp_get_nav_menu_items example


function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
    $menu = wp_get_nav_menu_object( $menu_id );

    // If the menu exists, get its items.     if ( is_nav_menu( $menu ) ) {
        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) );
        $result     = '<div id="menu-instructions" class="post-body-plain';
        $result    .= ( ! empty( $menu_items ) ) ? ' menu-instructions-inactive">' : '">';
        $result    .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
        $result    .= '</div>';

        if ( empty( $menu_items ) ) {
            return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
        }

        /** * Filters the Walker class used when adding nav menu items. * * @since 3.0.0 * * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'. * @param int $menu_id ID of the menu being rendered. */
if ( ! isset( $locations[ $location ] ) ) {
            return;
        }

        // Get the menu from the location, returning early if there is no         // menu or there was an error.         $menu = wp_get_nav_menu_object( $locations[ $location ] );
        if ( ! $menu || is_wp_error( $menu ) ) {
            return;
        }

        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
        _wp_menu_item_classes_by_context( $menu_items );

        return $menu_items;
    }


    /** * Sorts a standard array of menu items into a nested structure keyed by the * id of the parent menu. * * @param array $menu_items Menu items to sort. * @return array An array keyed by the id of the parent menu where each element * is an array of menu items that belong to that parent. */

    public static function convert( $menu ) {

        if ( ! is_nav_menu( $menu ) ) {
            return new WP_Error(
                'invalid_menu',
                __( 'The menu provided is not a valid menu.' )
            );
        }

        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );

        if ( empty( $menu_items ) ) {
            return '';
        }

        // Set up the $menu_item variables.         // Adds the class property classes for the current context, if applicable.         _wp_menu_item_classes_by_context( $menu_items );

        $menu_items_by_parent_id = static::group_by_parent_id( $menu_items );

        
$this->manager->add_setting(
                new WP_Customize_Nav_Menu_Setting(
                    $this->manager,
                    $nav_menu_setting_id,
                    array(
                        'transport' => 'postMessage',
                    )
                )
            );

            // Add the menu contents.             $menu_items = (array) wp_get_nav_menu_items( $menu_id );

            foreach ( array_values( $menu_items ) as $i => $item ) {

                // Create a setting for each menu item (which doesn't actually manage data, currently).                 $menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']';

                $value = (array) $item;
                if ( empty( $value['post_title'] ) ) {
                    $value['title'] = '';
                }

                
// 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;
    }

    
'menu-item-xfn'           => '',
        'menu-item-status'        => '',
        'menu-item-post-date'     => '',
        'menu-item-post-date-gmt' => '',
    );

    $args = wp_parse_args( $menu_item_data$defaults );

    if ( 0 == $menu_id ) {
        $args['menu-item-position'] = 1;
    } elseif ( 0 == (int) $args['menu-item-position'] ) {
        $menu_items                 = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
        $last_item                  = array_pop( $menu_items );
        $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
    }

    $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;

    if ( 'custom' === $args['menu-item-type'] ) {
        // If custom menu item, trim the URL.         $args['menu-item-url'] = trim( $args['menu-item-url'] );
    } else {
        /* * If non-custom menu item, then: * - use the original object's URL. * - blank default title to sync with the original object's title. */
case 'move-down-menu-item':
        // Moving down a menu item is the same as moving up the next in order.         check_admin_referer( 'move-menu_item' );

        $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;

        if ( is_nav_menu_item( $menu_item_id ) ) {
            $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );

            if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
                $menu_id            = (int) $menus[0];
                $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
                $menu_item_data     = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );

                // Set up the data we need in one pass through the array of menu items.                 $dbids_to_orders = array();
                $orders_to_dbids = array();

                foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
                    if ( isset( $ordered_menu_item_object->ID ) ) {
                        if ( isset( $ordered_menu_item_object->menu_order ) ) {
                            $dbids_to_orders[ $ordered_menu_item_object->ID ]         = $ordered_menu_item_object->menu_order;
                            $orders_to_dbids[ $ordered_menu_item_object->menu_order ] = $ordered_menu_item_object->ID;
                        }
Home | Imprint | This part of the site doesn't use cookies.