I've spent a couple of days googleling around, looking for a way to automatically add categories and authors to my custom menu as soon as they are created (the same way that pages are automatically added!), but so far no luck!

I'd really like for my client to not have to go to menu and add the category or the author to the menu manually every time a new one is created.

Any ideas?


1 Answer 1


You can use the filter hook wp_get_nav_menu_items to add new items in the WP Nav Menu. The follow example is to add the last posts in the nav menu.

You can add to this filter your custom function, like to add each post from author with a specific ID. The logic to add the posts is inside your custom function, like the follow example in replace_placeholder_nav_menu_item_with_latest_post.

// 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', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );

// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {

    // Loop through the menu items looking for placeholder(s)
    foreach ( $items as $item ) {

        // Is this the placeholder we're looking for?
        if ( '#latestpost' != $item->url )

        // Get the latest post
        $latestpost = get_posts( array(
            'numberposts' => 1,
        ) );

        if ( empty( $latestpost ) )

        // Replace the placeholder with the real URL
        $item->url = get_permalink( $latestpost[0]->ID );

    // Return the modified (or maybe unmodified) menu items array
    return $items;

The source example is from Viper007Bond, see the post for more information about the code.

  • Hi bueltge! I tried it with the Categories and indeed it lists the categories in the menu, but outside the main ul. You can see what I mean here: link . And the code that I used, replacing get_post for wp_list_categories: $latestpost = wp_list_categories( array( 'orderby' => name, 'show_count' => 0, 'exclude' => 3, 'title_li' => "", ) ); Jan 6, 2014 at 16:07

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.