Ok, the problem in my solution was the use of $menu_item->ID
instead of
$menu_item->object_id
. So the full correct logic is like so:
function print_sub_menus() {
global $post;
// get current page
$post_id = $post ? $post->ID : -1;
$has_post_parent = $post && $post->post_parent;
$top = $has_post_parent ? array_pop( get_post_ancestors($post_id) ) : $post_id;
// get all main menu items
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations['primary-menu']);
$menu_items = wp_get_nav_menu_items($menu->term_id);
echo '<div id="submenu" class="clearfix">';
foreach($menu_items as $menu_item) {
$current_id = $menu_item->object_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>';
}
echo '</div>';
}