Usualmente con las función wp_nav_menu de WordPress para mostrar menus es suficiente para cualquier trabajo (si aún no sabes utilizarla puedes verla en este tutorial), sin embargo hay algunos casos donde queremos tener total control sobre nuestro menu y sus clases o sólo queremos extraer una parte de este, para esos casos WordPress nos da una función que devuelve los elementos para luego poder mostrarlos mediante un array.

Sintaxis Básica

1
<?php $items = wp_get_nav_menu_items( $menu, $args ); ?>

Donde $menu viene a ser el ID del menu que deseamos mostrar y $args es un array con algunos parámetros que podemos personalizar como son:

1
2
3
4
5
6
7
8
'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 ); ?>

Ejemplo de Menu

Lo usual es que no se requiera más parámetro que el ID del menu y luego mediante un bucle mostramos los elementos pudiendo añadirles una clase, un ID o lo que se nos ocurra

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$menu_items = wp_get_nav_menu_items(10);

$menu_list = '
<ul id="menu-'
. $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '
<ul>
    <li><a href="'
. $url . '">' . $title . '</a></li>
</ul>
&nbsp;

'
;
}
$menu_list .= '</ul>';

Puedes ver la lista completa de atributos que devuelve el array como si es menu, submenu o tiene hijos AQUI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Object (
       ID = 2178
       post_author = "1"
       post_date = "2013-08-20 20:37:40"
       post_date_gmt = "2013-08-20 20:37:40"
       post_content = "This is the link description"
       post_title = "Example Category"
       post_excerpt = "My title attribute"
       post_status = "publish"
       comment_status = "open"
       ping_status = "open"
       post_password = ""
       post_name = "2178"
       to_ping = ""
       pinged = ""
       post_modified = "2013-08-20 20:13:08"
       post_modified_gmt = "2013-08-20 20:13:08"
       post_content_filtered = ""
       post_parent = 12
       guid = "http://example.com/?p=2178"
       menu_order = 2
       post_type = "nav_menu_item"
       post_mime_type = ""
       comment_count = "0"
       filter = "raw"
       format_content = NULL
       db_id = 2178
       menu_item_parent = "2177"
       object_id = "19"
       object = "category"
       type = "taxonomy"
       type_label = "Category"
       url = "http://example.com/category/example-category/"
       title = "Example Category"
       target = ""
       attr_title = "My title attribute"
       description = "This is the link description"
       classes => Array (
           ['0'] = "my-class"
       )
       xfn = "contact"
   )