Latest post in a specific menu

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

This is my menu:

Read More
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 😉

Related posts

1 comment

  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

Comments are closed.