1

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

4
  • Do you mean the default listing of posts which you see on the home page? Commented Jan 2, 2014 at 18:54
  • Hi, I need to show the latest post of a specific category in a specific menu. Commented Jan 2, 2014 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. Commented Jan 2, 2014 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/… Commented Jan 2, 2014 at 20:14

1 Answer 1

1

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

5
  • Thanks, can I also show the related thumbinal? Commented Jan 4, 2014 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. Commented Jan 4, 2014 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
    Commented Jan 4, 2014 at 12:23
  • Hi,Thanks but I have a trouble with the resize image, it show always the thumbnail 150x150 and not 24x24. Commented Jan 4, 2014 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
    Commented Jan 4, 2014 at 15:18

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.