0

I've got a function that ties in to wp_nav_menu that shows sibling pages for the currently viewed page, as well as any children pages. I need this modified to only show the children pages of a specific page, but I honestly don't understand how this works. What I'd like to be able to do is set a "master parent page" by ID in the wp_nav_menu function, and only show children of that page.

Here's my current function: .

// add sub_menu options to wp_nav_menu
function my_wp_nav_menu_objects_sub_menu($sorted_menu_items, $args) {
    if (isset($args->sub_menu)) {
        $root_id = 0;
        foreach ($sorted_menu_items as $menu_item) {
            if ($menu_item->current) {
                $root_id = ($menu_item->menu_item_parent) ? $menu_item->menu_item_parent : $menu_item->ID;
                break;
            }
        }
        if (!isset($args->direct_parent)) {
            $prev_root_id = $root_id;
            while ($prev_root_id != 0) {
                foreach ($sorted_menu_items as $menu_item) {
                    if ($menu_item->ID == $prev_root_id) {
                        $prev_root_id = $menu_item->menu_item_parent;
                        if ($prev_root_id != 0) $root_id = $menu_item->menu_item_parent;
                        break;
                    } 
                }
            }
        }
        $menu_item_parents = array();
        foreach ($sorted_menu_items as $key => $item) {
            if ($item->ID == $root_id) $menu_item_parents[] = $item->ID;

            if (in_array($item->menu_item_parent, $menu_item_parents)) {
                $menu_item_parents[] = $item->ID;
            } else if (!( isset($args->show_parent) && in_array($item->ID, $menu_item_parents))) {
                unset( $sorted_menu_items[$key]);
            }
        }
        return $sorted_menu_items;
    } else {
        return $sorted_menu_items;
    }
}
add_filter("wp_nav_menu_objects", "my_wp_nav_menu_objects_sub_menu", 10, 2);

And here's an example of how wp_nav_menu ties in to it:

wp_nav_menu(array(
    "container"      => "nav",
    "depth"          => 2,
    "items_wrap"     => "<ul>%3\$s</ul>",
    "sub_menu"       => true,
    "theme_location" => "primary",
));

Any help would be greatly appreciated.

0

To specifically target a page and show its children/siblings you can use wp_list_pages();

eg.

wp_list_pages(array(
    'child_of' => "page parent ID",
    'depth' => 2
));
  • Only problem is I need it to work along with wp_nav_menu rather than actual page structure, so that they only have to structure everything how they want it in one place. – JacobTheDev Feb 13 '15 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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