WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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:



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.

share|improve this question
up vote 0 down vote accepted

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 ) {

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


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;
    return array_merge( $items, $child_items );
share|improve this answer
Seems like it doesn't work :S I've inserted this: if( $item->ID === 45): // ADD THIS MENU-ITEM ID CHECKright above the "query the lasts" and changeded the foreach as you said... keeps the same dup! – Horacsio Jun 17 '13 at 15:27
It worked finally. I'm going to post the final result in the next 3 hours. Thank you birgire – Horacsio Jun 17 '13 at 15:43
ok great, I just posted my working solution ;-) – birgire Jun 17 '13 at 15:53
well your working solution as a mistake :D if( $item->ID === 45): it should end with a ; not with : can you correct it? And it's not the same script as mine! I'll post it later anyway ;) – Horacsio Jun 17 '13 at 15:58
I modified your script slightly ;-) (the : is actually not an error, it's part of the if sentence that ends with endif) – birgire Jun 17 '13 at 16:09

Your Answer


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.