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.

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.

share|improve this question
add comment

1 Answer

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 ) {
            //...
         }
    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 );
}
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! –  user2120775 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 –  user2120775 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 ;) –  user2120775 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
show 1 more 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.