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 have some posts categories as element of a main navigation menu, and i'd like to display the ten lasts posts of this category as sub menu.

ex: Category1 Lastpost1 Lastpost2

The main menu is build in the admin panel, and i'm wondering if there is a hook or something to achieve that automatically. I know how to hardcode that, but i'd like to keep using the cool menu edition from admin panel.

any ideas?

share|improve this question
add comment

1 Answer 1

I finally achieve it with this code in function.php

// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
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); /* Offset menu order */
$child_items = array();

// Loop through the menu items looking category menu object
foreach ( $items as $item ) {

    // Test if menu item is a categroy and has no sub-category
    if ( 'category' != $item->object || ('category' == $item->object && get_category_children($item->object_id)) )

    // Query the lasts ten category posts
    $category_ten_last_posts = array(
            'numberposts' => 10,
            'cat' => $item->object_id,
            'orderby' => 'date',
            'order' => 'DESC'

    foreach ( get_posts( $category_ten_last_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 );

This will display as sub menu, the ten last posts of a category placed as menu item or sub menu item in admin panel.

This code is highly inspired from this: http://codeseekah.com/2012/03/05/list-all-posts-in-wordpress-navigation-menu/

share|improve this answer
add comment

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.