最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Assign a different menu depending on custom taxonomy

programmeradmin2浏览0评论

I've been googling, tinkering around and investigating/testing settings however nothing seems to work so please bear with me on this and please help me if you can. Thanks so much!

I have the custom taxonomy called "location" created via the CPT UI plugin - below is the code used by CPT UI plugin to register new taxonomy "location".

function cptui_register_my_taxes_location() {

    /**
     * Taxonomy: Locations.
     */

    $args = array(
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'location', 'with_front' => true, ),
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "location",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => true,
        );
    register_taxonomy( "location", array( "post", "blog" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_location' );

Now here is the issue I'm having trouble finding the solution for: is there a way to assign a set of menu depending on what taxonomy of the page is?

Ideally, I'll create a set of menu under Appearance > Menus and name it "Test Menu" and then assign it to a specific taxonomy term - is there a wordpress code (function) that we can all use wherein we'll set the TAXONOMY name and the matching MENU name?

Thanks so much guys in advance for your help!

I've been googling, tinkering around and investigating/testing settings however nothing seems to work so please bear with me on this and please help me if you can. Thanks so much!

I have the custom taxonomy called "location" created via the CPT UI plugin - below is the code used by CPT UI plugin to register new taxonomy "location".

function cptui_register_my_taxes_location() {

    /**
     * Taxonomy: Locations.
     */

    $args = array(
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'location', 'with_front' => true, ),
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "location",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => true,
        );
    register_taxonomy( "location", array( "post", "blog" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_location' );

Now here is the issue I'm having trouble finding the solution for: is there a way to assign a set of menu depending on what taxonomy of the page is?

Ideally, I'll create a set of menu under Appearance > Menus and name it "Test Menu" and then assign it to a specific taxonomy term - is there a wordpress code (function) that we can all use wherein we'll set the TAXONOMY name and the matching MENU name?

Thanks so much guys in advance for your help!

Share Improve this question asked Jun 28, 2019 at 16:11 BacorritoBacorrito 54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think the way to do this is:

  1. Set up your menu in Appearance > Menus as you mentioned;
  2. In your theme's header.php (or wherever you find the wp_nav_menu() code that displays your current menu), set up a WordPress conditional to check if the page being visited is a location taxonomy archive, and display different menus based on that. For instance:

    if (is_tax('location')) { wp_nav_menu( array($args = array( 'menu' => 'test-menu', // your menu slug here ) ); } else { wp_nav_menu(); // your original menu code here }

See the Conditionals page in the WordPress codex for all the options you could use here, there are many.

发布评论

评论列表(0)

  1. 暂无评论