How to list recent posts in a wp nav menu?

I’ve been trying this for some time and didn’t get no clear answer.

I need to display 11 recent posts inside one of my menu items. My menu is like:

ITEM 1 · MAMBA · ITEM 3

I need to display post titles and respective url’s when rollover MAMBA. I got this code from Joeyjoejoe’s Category menu item and its last 10 posts as sub-menu but pasted as is I can get recent posts for all submenu items as categories.

I had to change it a bit to appear only in MAMBA:

$category_ten_last_posts = array(
    'showposts' => 11,
    'category_name' => 'mamba',

and the target item:

$post->menu_item_parent = 45;

saddly the code started to repeat the recent posts list, in a duplicated maner:

Mamba

……….

Post 16

Post 15

Post 16

Post 15

My question here is how to list a controlled number of recent posts from a choosen category to a specific Menu Item?

Thank you for all the help you can give.

Best regards.
H.

Solutions Collecting From Web of "How to list recent posts in a wp nav menu?"

What about checking the ID value of each menu item like the following:

   if( $item->ID === 45):  // ADD THIS MENU-ITEM ID CHECK
        // Query the lasts ten category posts
        $category_ten_last_posts = array(
             'posts_per_page' => 11,
             'category_name' => 'mamba',
             'orderby' => 'date',
             'order' => 'DESC'
        );
        $posts = get_posts( $category_ten_last_posts );
        foreach ( $posts  as $post ) {
            //...
         }
    endif;

ps: I moved the get_posts() out of the foreach input argument.

Update

This works on my install:

!is_admin() AND add_filter( 'wp_get_nav_menu_items', 'display_lasts_ten_posts_for_categories_menu_item', 10, 3 );

// Add the ten last posts of af categroy menu item in a sub menu
function display_lasts_ten_posts_for_categories_menu_item( $items, $menu, $args ){
    $menu_order = count($items); 
    $child_items = array();
    foreach ( $items as $item ):
        if( $item->ID === 45 ): 
            // Query the lasts ten category posts
            $category_ten_last_posts = array(
                'posts_per_page' => 11,
                'category_name'  => 'mamba',
                'orderby'        => 'date',
                'order'          => 'DESC'
            );
            $posts = get_posts( $category_ten_last_posts );
            foreach( $posts as $post ):
                // Add sub menu item
                $post->menu_item_parent = $item->ID;
                $post->post_type        = 'nav_menu_item';
                $post->object           = 'custom';
                $post->type             = 'custom';
                $post->menu_order       = ++ $menu_order;
                $post->title            = $post->post_title;
                $post->url              = get_permalink( $post->ID );
                /* add children */
                $child_items[]          = $post;
            endforeach;
        endif;
    endforeach;
    return array_merge( $items, $child_items );
}