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.

is it possible to show the latest posts only in a specific menu?

This is my menu:

register_nav_menus(array(
'primary' => __('Primary Navigation', 'gonzo'),
'mobile' => __('Mobile Navigation', 'gonzo'),
'copyright' => __('Footer Copyright Menu', 'gonzo'),
'toplevel' => __('Top Secondary Menu', 'gonzo')
 ));

modifying this code

I want to show it only in "primary" menu. Thanks ;)

share|improve this question
    
Do you mean the default listing of posts which you see on the home page? –  Chittaranjan Jan 2 at 18:54
    
Hi, I need to show the latest post of a specific category in a specific menu. –  Francesco Craparo Jan 2 at 19:11
    
I am not sure if I understood this correctly. You can add a menu item to any of the menu you like and select a category from the categories list on the left for that specific menu item. After saving the menu, you can check on the site and that menu link will show the latest posts for that specific category. –  Chittaranjan Jan 2 at 19:35
    
i have a category (id = 46) when i write posts on this category i would like to show the last 5 posts into the menu of header (i have 4 menus and i want to show those only in the "primary" menu). Look at [link]wordpress.stackexchange.com/questions/56250/… –  Francesco Craparo Jan 2 at 20:14

1 Answer 1

up vote 1 down vote accepted

Yes, this is possible:

function latest_posts_menu($items, $args) {
//To a Specific Menu
if ($args->theme_location == 'primary') {

    // Parent Menu Item
    $items .= '<li id="menu-item" class="menu-item">
                <a href="#">' . __('Latest Posts', 'textdomain') . '</a>
                    <ul class="sub-menu">';
    // Create Sub Menus
    foreach ( get_posts(
                array(
                    'posts_per_page'=>10,
                    'post_type' => array('post')
                    ) 
            ) as $post ) {
            // Menu Thumbnail. You can change the dimenssion as well as the image class
            $thumb = get_the_post_thumbnail( $post->ID, array(24,24), array("class" => "menu-thumb") );
        $items .= '<li class="sub-menu-item">
                        '.$thumb.'
                        <a href="'.get_permalink( $post->ID ).'">'. $post->post_title .'</a>
                    </li>';
    }
    // Close The Menu
    $items .= '</ul></li>';
}
    return $items;
}
add_filter( 'wp_nav_menu_items', 'latest_posts_menu', 10, 2 );

Source: Display Latest Posts In Menu Item

share|improve this answer
    
Thanks, can I also show the related thumbinal? –  Francesco Craparo Jan 4 at 10:54
    
This code is good but it is not working fine with UberMenu because it not show the thumbnail and it needs to modified the classes manually. –  Francesco Craparo Jan 4 at 11:55
    
Of course you can show related thumbnail. Just add the thumbnail as featured image. Code corrected to display the featured image before submenu. –  Abhik Jan 4 at 12:23
    
Hi,Thanks but I have a trouble with the resize image, it show always the thumbnail 150x150 and not 24x24. –  Francesco Craparo Jan 4 at 13:22
    
Put this on functions.php add_image_size('menuthumbs' 24, 24, true);. Then replace the dimension array (array(24,24))with the thumbnail ID (i.e 'menuthumbs') in get_the_post_thumbnail function. All you need to do after that is rebuild the thumbnail using a plugin. –  Abhik Jan 4 at 15:18

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.