Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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>';
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.