Can I use following script to build my primary menu which includes items of many other menus?
Example: my main menu looks like this -
-1
--1.1
--1.2
-2
and I have many other menus with ID = (1.1, 1.2, ......), so while building menu on a webpage it should combine all matching menus. Result should be like this:
-1
--1.1
---items from menu id 1.1
--1.2
---items from menu id 1.2
-2
and so on.
Here is a code that I have -
// Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
// This code based on wp_nav_menu's code to get Menu ID from menu slug
$menu_name = 'custom_menu_slug';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$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 .= '<li><a href="' . $url . '">' . $title . '</a></li>';
}
$menu_list .= '</ul>';
} else {
$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
}
// $menu_list now ready to output
source - http://ift.tt/1xMgu0C
My aim is to break down large menu into smaller ones and then build them up on my pages.
Aucun commentaire:
Enregistrer un commentaire