【问题标题】:Taxonomy link not showing in custom admin menu wordpress自定义管理菜单 wordpress 中未显示分类链接
【发布时间】:2014-10-07 01:13:27
【问题描述】:

我为此找到了许多解决方案,但我的情况完全不同。

现在我的问题是,我正在开发一个插件,在插件中我制作自定义帖子类型和自定义分类,首先它的 slug 不同,现在我希望帖子类型 slug 和分类 slug 相同,我也成功实现了这一点但是有一个问题是我的类别链接没有显示在管理菜单中,因为当我注册分类时,我将 object_type 设置为 slug,这在帖子类型 slug 和分类 slug 中是相同的,但是当我将对象类型更改为帖子类型名称类别菜单时完美呈现。

这是我的代码

帖子类型代码:

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();
}

分类代码:

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();
}

这是代码,所以我的帖子类型 slug 和分类学 slug 是相同的:

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' );

这是我的菜单代码:

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');
}

现在,当我更改 register_taxonomy( 'kbe_taxonomy', array('kbe_articles'), 时,我的分类链接会出现在管理自定义菜单中,但是当我更改 register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), 时,分类链接会从菜单中消失。

那么我该怎么做才能让我的分类链接出现在菜单中。但不改变蛞蝓。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    好的,这是答案。

    我在一个条件下再添加一个子菜单

    这是我的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      相关资源
      最近更新 更多