6

Is it possible to print content of all menu items, when displaying menu (or branch of menu as in question about displaying menu branches) ?

enter image description here

Once I click on About Us I wish for new page to display content of all it's children links.

So basically I am looking for a way to get IDs of those posts/pages and use them inside my WP Query.

2
  • Looking at your edit - so which one is it: displaying content when rendering menu or just retrieving IDs? Please clarify and if latter we need to adjust title again. :)
    – Rarst
    Commented Aug 23, 2012 at 18:29
  • you think wp_get_associated_nav_menu_items() might help? Commented Aug 23, 2012 at 22:35

1 Answer 1

4

I am lazy to write supporting logic from scratch so I am reusing functions from linked answer on branches:

/**
 * Retrieve IDs of posts in branch of menu.
 *
 * @param mixed  $menu
 * @param string $branch_title
 *
 * @link http://wordpress.stackexchange.com/questions/2802/display-a-portion-branch-of-the-menu-tree-using-wp-nav-menu
 *
 * @return array
 */
function get_post_ids_from_menu_branch( $menu, $branch_title ) {

    $menu_object = wp_get_nav_menu_object( $menu );
    $menu_items  = wp_get_nav_menu_items( $menu_object->term_id );
    $items       = submenu_limit( $menu_items, (object) array( 'submenu' => $branch_title ) );
    $items       = wp_list_filter( $items, array( 'object' => 'post' ) );
    $ids         = wp_list_pluck( $items, 'object_id' );

    return $ids;
}

// example
var_dump( get_post_ids_from_menu_branch( 'Test menu', 'Level 1' ) );
1
  • WOW! That's some really good news. Never heard of wp_list_filter & wp_list_pluck but this is just what I was after. Many thanks! You saved the day ;) Commented Aug 24, 2012 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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