How to Get Menu Items In Wordpress?

2019-02-20 21:54发布

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.

Thanks.

5条回答
地球回转人心会变
2楼-- · 2019-02-20 22:07

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/

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-20 22:10

Use get_registered_nav_menus() function

<?php

$menus = get_registered_nav_menus();

foreach ( $menus as $location => $description ) {

    echo $location . ': ' . $description . '<br />';
}
查看更多
混吃等死
4楼-- · 2019-02-20 22:14
<?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 ); 
?> 
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-20 22:18

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.

查看更多
不美不萌又怎样
6楼-- · 2019-02-20 22:24

You can try this

$menu_name = 'sidebar-menu'; //menu slug
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

echo "<pre>";
print_r($menuitems);
echo "</pre>";

You will get your menu Object

refer : http://wiki.workassis.com/wordpress-get-menu-array/

查看更多
登录 后发表回答