I have a 2 part question. This is my first post here, so apologies if I am doing it wrong.

I have a custom taxonomy (created with WP Toolset) called 'Author Categories'.

What I need to have done is this:

menu item | menu item | authors | menu item

I have a menu item called 'authors' which leads to a page called 'authors' and it has all of the authors listed under certain categories (eg. thriller, children's book, etc.), these are the categories from my custom taxonomy.

Question 1: How can I have the categories be added in a dynamic manner (eg. when a new category is created it will list it in the dropdown)?

Question 2: How can I link each of these categories in this menu dropdown to this same 'authors' page, but add a #slug after the URL so that it can scroll to the right category on that page? I have already made sure each category title on that 'authors' page has the slug set as an ID of the element.

Sorry if I ask a lot, but I have done days of research and can't figure it out, also in part as in my opinion the documentation in the Wordpress Codex is not complete enough.

Thanks for any help!

1 Answer 1


If your menu is generated via wp_nav_menu, you can use a Custom Walker to add a dynamic submenu to a specific item.

First, the class, which would go in your theme's functions.php file, or your own custom plugin:

class Walker_Author_Categories extends Walker_Nav_Menu {
    // we only need to override the function that closes each menu item,
    // this is where we can insert a submenu
    function end_el( &$output, $item, $depth=0, $args=array() ) {
        // if the current menu item being output is authors
        if( 'authors' == $item->title ){
            // get all of the author categories
            $author_cats = get_terms( array(
                'taxonomy' => 'author_categories',
                'hide_empty' => false,
            ) );
            if ( ! empty( $author_cats ) && ! is_wp_error( $author_cats ) ) {
                // url of the authors page
                // to be used to append #anchor links
                $parent_url = get_permalink( $item->object_id );
                // start a new list
                $output .= '<ul>';
                // iterate over each category
                // and add an li
                foreach( $author_cats as $author_cat ){
                    $slug = $author_cat->slug;
                    $name = $author_cat->name;
                    $format = '<li><a href="%s#%s">%s</a></li>';
                    $output .= sprintf( $format, $parent_url, $slug, $name );
                // close the list
                $output .= '</ul>';
        // close the parent li
        $output .= "</li>\n";  

Then, where you output the menu, add the custom walker:

wp_nav_menu( array(
    'theme_location' => 'primary',
    'walker' => new Walker_Author_Categories
) );
  • Great! Thanks, Milo! I was looking into Walkers but did not quite grasp how they work yet. I will try this. Commented Feb 7, 2019 at 6:11
  • That works perfectly! I only needed to change title to Author (capitalized) and change the theme location according to my theme. Thanks again, Milo!! Commented Feb 7, 2019 at 8:03

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.