Category in menu populated with recent posts submenus

  • Author
  • #1790883


    I set up a blog with about a month ago and really enjoy it! Despite the limitations (no custom plugin, no access to the PHP code of themes, etc.), the available features are amazing and makes blogging very easy and connected with other wordpress bloggers.

    However, I would like to know if it would be possible to implement an option to “populate” the category menu items of my blog automatically with links to recent post as submenus. In other words, when my visitors hover a category in the menu, I would like my category menu item to display the latest 10 posts which belong to this category without me having to do it myself through the admin panel. From now on, I’ve been doing this manually, but I found it pretty cumbersome and it isn’t convenient when I schedule posts to be published in the future (I would like the submenu items to be immediately visible when the post becomes public).

    I found a PHP tweak on the Internet that allows blogs hosted on servers to do it. The code that goes to the function.php page is the following, but I can not use it on my blog:

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

    Would it be possible to implement this function in the future? Or is there another automated method that I don’t know of?


    The blog I need help with is



    Have you read these sections of the support documentation?



    Hi timethief,

    I have now. But I stil don’t see anything that would let me do what I want: Recent posts displayed automatically (as opposed to me manually adding them) in the menu as submenu items of the category to which they belong.

    I don’t know if I’m stating things the right way.

    I have a new post coming up, and it will be showed on the front page, and on the category page as well. But what I would like to have is a link under the category menu to which it belongs to show up without me adding it by hand in the admin panel.



    You aren’t going to get that here.



    Maybe we are not understanding each other. If so, then I apologize.

    I want: Recent posts displayed automatically (as opposed to me manually adding them) in the menu as submenu items of the category to which they belong.

    There is only one page in any blog that we can post to and by default it’s the front page of the blog. Though we cannot post to more than one page we can create the appearance we have done so. What’s critical is knowing that the static pages we blogger create will never automatically update. Only dynamic Categories pages will automatically update when we publish posts.

    Below is an example of a static page URL. Study it closely and note that no static Page can automatically update.

    Below is an example of a category URL.
    Study it closely please and note that only dynamic category pages can automatically update.

    Setting up a menu is easy as pie, provided you know the differences between Posts and Pages, and the differences between static Pages, (which will never automatically update) and dynamic Categories pages which will automatically update when you publish Posts with Categories assigned to them.

    The relevant support docs for creating custom menus, adding categories and sub-categories pages into it and ordering them to create drop downs are at:

    Read this carefully

    There are many common errors, misunderstandings and misconceptions when creating custom menus and there are some tweaks you can use to improve your custom menu as well.


    Thank you for your help. I might not use the right terminology, so I will try to rephrase it.

    If you look at the menu structure of my blog ( you will see:

    First, the home link. But that is not relevant to my question.
    Then, category links: Each dynamic category page of my blog (capoeira, practice, music, internet, and misc) has a menu item that links to this dynamic category page (if you click on “practice” in the menu, you will see the dynamic category page that displays posts categorized under “practice”).
    Finally, two custom page links named “links” and “this blog” which lead to static pages. These two links are not relevant to my question either.

    What I want to do is about the submenu items of each category link. I would like my recent posts to show up as submenu links below the category links depending on which category these posts belong to. Let’s say I publish a new post categorized as “music.” I would like to find a way to automatically have a submenu link appear below the “music” category link named after my post title that links to the post.

    So far, I did it manually: When I publish a new post, I create a submenu link (visible as a “child” of the category link to which the post belongs) that links to my post. But I would like to find a way to do that automatically: I publish a new post, and a submenu link to this post appears under the right category link of the menu. Does it make sense?



    Hi again,
    I’m going to tag this thread for a Staff response. Please subscribe to the thread so you are notified when they respond and please be patient while waiting.


    Thank you, timethief! I did subscribe already. And, yes, I’ll be patient. I guess I felt like I had a hard time explaining myself–I also were pretty available during this exchange, so that is why I responsed so quickly. I hope that I didn’t offend you. I’m really happy about and do not intend to troll or anything like that.



    Hi there!

    Thanks for the feature suggestion! Right now I recognize that this isn’t something that’s currently possible and implementing it can be difficult as it will require hacking into our core files. For the moment what I can suggest is to create a text widget and insert the Display Posts Shortcode to display posts based on a filter, say a category.

    For its usage, check out our support page:

    Meanwhile, I went ahead and created a sample widget for you called “Text Widget: Practice”. Feel free to play around with it and experiment.

    Hope that helps! Let me know if you have more questions. Cheers. :)



    Thank you very much for your help. I didn’t know of this shortcode, and I like it very much, even if it doesn’t do what I wanted in the first place.

    I believe that it would be nice to implement this little PHP tweak as an option (a box that could be ticked in the menu settings) in the menu on
    What do you think? Could it actually be implemented some day?



    What do you think? Could it actually be implemented some day?

    Really tough to say at the moment. I do think it’s a good idea and will certainly let our developers know about it. Thanks again! :)

The topic ‘Category in menu populated with recent posts submenus’ is closed to new replies.