Automatically adding categories and authors in custom menu?

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.

Read More

Any ideas?

Related posts

1 comment

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

Comments are closed.