$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Add custom taxonomy terms to Wordpress menu dynamically & append #slug to url|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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
) );
Post a comment

comment list (0)

  1. No comments so far