我为此找到了很多解决方案,但我的情况完全不同。
现在我的问题是,我正在开发插件和插件我制作自定义帖子类型和自定义分类,首先它的slugs是不同的,现在我想要那个post类型slug和分类学slug是相同的,我也成功实现了这一点但是有一个问题是我的类别链接没有在管理菜单中显示,因为当我注册分类法时我将object_type设置为slug,它在post类型slug和taxonomy slug中是相同的但是当我将对象类型更改为post type name category menu时完美呈现。
这是我的代码
邮政类型代码:
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),
分类法链接消失时。
那么我能做什么,我的分类链接出现在菜单中。但不要改变slu ..
答案 0 :(得分:2)
好的,这是答案。
我在条件
上再添加一个子菜单这是我的代码:
add_submenu_page('edit.php?post_type=foo_articles', 'Categories', 'Categories', 'manage_options', 'edit-tags.php?taxonomy=foo_taxonomy&post_type=foo_articles');