categories - Add custom taxonomy terms to Wordpress menu dynamically & append #slug to url

admin2025-06-03  3

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
                        category
                        category
                        category
                        etc.

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!

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
                        category
                        category
                        category
                        etc.

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!

Share Improve this question edited Feb 7, 2019 at 4:42 Janne Wolterbeek asked Feb 7, 2019 at 3:21 Janne WolterbeekJanne Wolterbeek 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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
) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748936601a314969.html

最新回复(0)