简体   繁体   中英

Taxonomy link not showing in custom admin menu wordpress

I found many solutions for this but my scenario is total different.

Now my issue is, i am working on a plugin and in plugin i make custom post type and custom taxonomy, firstly its slugs are different, now i want that post type slug and taxonomy slugs are same and i also achieve this successfully but there is a one problem that is my category link is not showing in admin menu because when i register taxonomy i set object_type as slug which is same in post type slug and taxonomy slug but when i change the object type as post type name category menu showing perfectly.

Here is my code

Post Type Code:

add_action('init', 'kbe_articles');
function kbe_articles() {
    $labels = array(
        'name'                  => __('Articles', 'kbe'),
        'singular_name'         => __('Articles', 'kbe'),
        'all_items'             => __('Articles', 'kbe'),
        'add_new'               => __('New Article', 'kbe'),
        'add_new_item'          => __('Add New Article', 'kbe'),
        'edit_item'             => __('Edit Article', 'kbe'),
        'new_item'              => __('New Article', 'kbe'),
        'view_item'             => __('View Articles', 'kbe'),
        'search_items'          => __('Search Articles', 'kbe'),
        'not_found'             => __('Nothing found', 'kbe'),
        'not_found_in_trash'    => __('Nothing found in Trash', 'kbe'),
        'parent_item_colon'     => ''
    );

    $kbe_rewrite = array(
        'slug'                => KBE_PLUGIN_SLUG,
        'with_front'          => false,
        'pages'               => true,
        'feeds'               => true,
    );

    $args = array(
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'query_var'             => true,
        'menu_icon'             => WP_Articles.'images/icon-kbe.png',
        'capability_type'       => 'post',
        'hierarchical'          => false,
        'menu_position'         => 3,
        'supports'              => array('title','editor','thumbnail','comments'),
        'rewrite'               => $kbe_rewrite,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => true
    );

    register_post_type( 'kbe_articles' , $args );
    flush_rewrite_rules();
}

Taxonomy Code:

add_action( 'init', 'kbe_taxonomies');
function kbe_taxonomies() {

    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => __( 'Articles Category', 'kbe'),
        'singular_name'     => __( 'Articles Category', 'kbe' ),
        'search_items'      => __( 'Search Articles Category', 'kbe' ),
        'all_items'         => __( 'All Articles Categories', 'kbe' ),
        'parent_item'       => __( 'Parent Articles Category', 'kbe' ),
        'parent_item_colon' => __( 'Parent Articles Category:', 'kbe' ),
        'edit_item'         => __( 'Edit Articles Category', 'kbe' ),
        'update_item'       => __( 'Update Articles Category', 'kbe' ),
        'add_new_item'      => __( 'Add New Articles Category', 'kbe' ),
        'new_item_name'     => __( 'New Articles Category Name', 'kbe' ),
    );  

    register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), array(
        'hierarchical'          => true,
        "label"                 => 'Categories',
        "singular_label"        => "Articles Category",
        'show_ui'               => true,
        'show_admin_column'     => true,
        'show_in_nav_menus'     => true,
        'show_tagcloud'         => true,
        'query_var'             => true,
        'rewrite'               => array( 'slug' => KBE_PLUGIN_SLUG, 'with_front' => true ),
    ));

    flush_rewrite_rules();
}

And this is the code so my post type slug and taxonomy slug are same:

function taxonomy_slug_rewrite($wp_rewrite) {
    $rules = array();
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');

    foreach ($post_types as $post_type) {
        $post_type_data = get_post_type_object( $post_type );
        $post_type_slug = $post_type_data->rewrite['slug'];

        foreach ($taxonomies as $taxonomy) {
            if ($taxonomy->object_type[0] == $post_type_slug) {
                $categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
                /* @var $category type */
                foreach ($categories as $category) {
                    $rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
                }
            }
        }
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );

And here is my menu code:

add_action('admin_menu', 'kbe_plugin_menu');
function kbe_plugin_menu() {
    add_submenu_page('edit.php?post_type=kbe_articles', 'Order', 'Order', 'manage_options', 'kbe_order', 'wp_kbe_order');
    add_submenu_page('edit.php?post_type=kbe_articles', 'Settings', 'Settings', 'manage_options', 'kbe_options', 'wp_kbe_options');
}

Now when i change register_taxonomy( 'kbe_taxonomy', array('kbe_articles'), My taxonomy link appears in admin custom menu but when i change register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), taxonomy link disapears from menu.

So what can i do that my taxonomy link appear in menu. but not change slugs.

Ok here is answer.

I add one more sub menu on a condition

here is my code:

add_submenu_page('edit.php?post_type=foo_articles', 'Categories', 'Categories', 'manage_options', 'edit-tags.php?taxonomy=foo_taxonomy&post_type=foo_articles');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM