How to Get Menu Items In WordPress?

I want to get items of my wp primary menu. I tried some code but all of them are returns only blank page. I don’t know which method I must use and don’t know need include any other page to my page?

And Is there any way to get menu items from database directly without using wp methods and functions? I couldn’t understand table structure of wp for now. So I don’t know relationships between tables exactly.

Read More

Thanks.

Related posts

Leave a Reply

5 comments

  1. If you know your menu location id (usually declared in functions.php by “register_nav_menus”) you can use this snippet:

    // GET ALL MENU OBJECTS AT SPECIFIED LOCATION
    function yourprefix_get_menu_items($location_id){
        //$locations = get_registered_nav_menus();
        $menus = wp_get_nav_menus();
        $menu_locations = get_nav_menu_locations();
    
        if (isset($menu_locations[ $location_id ]) && $menu_locations[ $location_id ]!=0) {
            foreach ($menus as $menu) {
                if ($menu->term_id == $menu_locations[ $location_id ]) {
                    $menu_items = wp_get_nav_menu_items($menu);
                    break;
                }
            }
            return $menu_items;
        }
    }
    

    Or more short version from codex:

    function yourprefix_get_menu_items($menu_name){
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
            return wp_get_nav_menu_items($menu->term_id);
        }
    }
    

    Then do everything you want with this array like so:

    $menu_items = yourprefix_get_menu_items('sidebar-menu'); // replace sidebar-menu by desired location
    
    if(isset($menu_items)){
            foreach ( (array) $menu_items as $key => $menu_item ) {
                ...some code...
            }
    }
    

    And here is link about all nav_menu data which you can select directly from database by mysql request:
    http://lasota.community.uaf.edu/2011/07/29/nav-menu-data-location-in-wordpress-3-2/

  2. <?php 
    $menu = 'menu-name/menu-id';
    $args = array(
            'order'                  => 'ASC',
            'orderby'                => 'menu_order',
            'post_type'              => 'nav_menu_item',
            'post_status'            => 'publish',
            'output'                 => ARRAY_A,
            'output_key'             => 'menu_order',
            'nopaging'               => true,
            'update_post_term_cache' => false );
    $items = wp_get_nav_menu_items( $menu, $args ); 
    ?> 
    
  3. You could do this:

    add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
    function user_nav_menu_items( $items ) {
        /* do something with $items (string) */
        return $items;
    }
    

    If you want it as an array, just explode it and implode it after you are done:

    add_action('wp_nav_menu_menuname_items', 'user_nav_menu_items');
    function user_nav_menu_items( $items ) {
        $items_r = explode("<li", $items);
        /* do something with $items_r (array) */
        $items_new = implode("<li", $items_r);
        return $items_new;
    }
    

    In both cases, you would to replace menuname in the add_action hook for the name of the menu you are trying to get the items of.