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'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?

share|improve this question
    
Does this help: wordpress.stackexchange.com/q/44736/21376 ? –  s_ha_dum Jan 6 at 15:19
add comment

1 Answer

up vote 0 down vote accepted

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

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

        if ( empty( $latestpost ) )
            continue;

        // 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.

share|improve this answer
    
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' => "", ) ); –  Eric Mitjans Jan 6 at 16:07
add comment

Your Answer

 
discard

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.