admin管理员组

文章数量:1317579

I registered a taxonomy named districts, but when I add districts to nav menus, the nav menu items are invalid! Here is the code that registers the taxonomy:


    function masallah() {

    // Add new taxonomy, make it hierarchical like categories
    //first do the translations part for GUI

        $labels = array(
            'name' => _x( 'Districts', 'taxonomy general name' ),
            'singular_name' => _x( 'District', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Districts' ),
            'all_items' => __( 'All Districts' ),
            'parent_item' => __( 'Parent Districts' ),
            'parent_item_colon' => __( 'Parent District:' ),
            'edit_item' => __( 'Edit District' ),
            'update_item' => __( 'Update District' ),
            'add_new_item' => __( 'Add New District' ),
            'new_item_name' => __( 'New Topic District' ),
            'menu_name' => __( 'Districts' ),
        );

    // Now register the taxonomy

        register_taxonomy('Districts',array('post'), array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'show_in_rest' => true,
            'rewrite' => true,
            'rewrite' => array( 'slug' => 'districts' ),
            'has_archive' => true,
        ));

    }

    add_action( 'init', 'masallah', 0 );

I registered a taxonomy named districts, but when I add districts to nav menus, the nav menu items are invalid! Here is the code that registers the taxonomy:


    function masallah() {

    // Add new taxonomy, make it hierarchical like categories
    //first do the translations part for GUI

        $labels = array(
            'name' => _x( 'Districts', 'taxonomy general name' ),
            'singular_name' => _x( 'District', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Districts' ),
            'all_items' => __( 'All Districts' ),
            'parent_item' => __( 'Parent Districts' ),
            'parent_item_colon' => __( 'Parent District:' ),
            'edit_item' => __( 'Edit District' ),
            'update_item' => __( 'Update District' ),
            'add_new_item' => __( 'Add New District' ),
            'new_item_name' => __( 'New Topic District' ),
            'menu_name' => __( 'Districts' ),
        );

    // Now register the taxonomy

        register_taxonomy('Districts',array('post'), array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'show_in_rest' => true,
            'rewrite' => true,
            'rewrite' => array( 'slug' => 'districts' ),
            'has_archive' => true,
        ));

    }

    add_action( 'init', 'masallah', 0 );
Share Improve this question edited Mar 26, 2020 at 13:51 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Mar 26, 2020 at 1:19 knockknockknockknock 132 bronze badges 12
  • You need to write your question. You can't be asking questions with YouTube videos. – Jacob Peattie Commented Mar 26, 2020 at 1:29
  • @JacobPeattie I dont know how to code videos, can I paste the code that comes from view button on winrar? Also I am not sure that you re-solve these code if you are not NEO from Matrix – knockknock Commented Mar 26, 2020 at 1:41
  • 1 Just describe the problem you're having! With words! – Jacob Peattie Commented Mar 26, 2020 at 2:14
  • Questions must be self contained, if viewing the youtube video is necessary to even know what the question is then it will be closed as unclear what you're asking. It does not matter how effective or good the video is, the question must contain the question, in words, in english. – Tom J Nowell Commented Mar 26, 2020 at 13:13
  • Having viewed the video, the only thing I can see wrong with your code is that you define rewrite twice, but that would not cause the problems you are seeing, and it would not cause a 000 taxonomy to appear. You need to switch to the default theme and disable all plugins, then re-enable them one by one to identify the plugin causing this, it is not standard WP behaviour. This also looks very familiar, did you ask this question using another account? – Tom J Nowell Commented Mar 26, 2020 at 13:16
 |  Show 7 more comments

1 Answer 1

Reset to default 0

I've identified two issues, the first, this:

            'rewrite' => true,
            'rewrite' => array( 'slug' => 'districts' ),

Can just be this:

            'rewrite' => array( 'slug' => 'districts' ),

And the second, the reason you get invalid menu items, is because districts is being used as the rewrite slug, but, the internal namevof the taxonomy is Districts.

Replacing Districts with districts as the internal name fixed the issue for me locally:

register_taxonomy( 'districts', array('post'), array(

It's a general rule of thumb to keep internal names of things lower case, you can always use the labels for what the user sees

本文标签: plugin developmentInvalid Menu Items