For a custom jquery menu I'd like to show all submenus and hide / show each on demand. However my problem is to show all submenus in the first place.
As of now I am managing the first level through the admin menu section: adding menu items to primary menu. Display works like so:
wp_nav_menu(
array (
'sort_column' => 'menu_order'
, 'container' => 'ul'
, 'menu_id' => 'main-nav'
, 'theme_location' => 'primary-menu'
, 'depth' => 1
)
);
The second level is (unfortunately) managed through the pages section with post_parent and menu_order. Therefore displaying it works like so:
global $post;
$has_post_parent = $post && $post->post_parent;
$post_id = $post ? $post->ID : -1;
$top = $has_post_parent ? array_pop( get_post_ancestors($post_id) ) : $post_id;
echo '<div id="submenu" class="clearfix"><ul>';
wp_list_pages(
array(
'child_of' => $top
, 'depth' => 1
, 'title_li' => ''
)
);
echo '</ul></div>';
I already tried to loop through the main menu items (first-level) like the following, but that left me with empty submenus, even though the main items seemed correct at first glance:
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations['primary-menu']);
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach($menu_items as $menu_item) {
$current_id = $menu_item->ID;
$is_current_class = ($current_id == $top) ? 'current-submenu' : '';
echo "<ul class='$is_current_class' data-parent='$current_id'>";
wp_list_pages(
array(
'child_of' => $current_id
, 'depth' => 1
, 'echo' => 1
, 'title_li' => ''
)
);
echo '</ul>';
}
EDIT
Even though I have fixed my initial solution, I am still very interested in a solution, where the menu can be completely managed through the wp-admin menus section.