使用add_menu_page
和add_submenu_page
通常用于向仪表板添加页面以达到特定目的,例如访问插件设置的选项页面。但是,如果您的目的是向自定义帖子类型(CPT)添加/编辑帖子/标签/类别,那么使用它们是不必要的。当CPT正确注册并具有相应的分类法时,仪表板将根据注册CPT/tax时在$args中设置的条件,默认情况下在侧栏中显示这些菜单选项。
下面是创建名为“store\\u product”的自定义帖子类型的代码,该类型使用了名为“product\\u category”的关联分类法的分层组织结构。请注意,层次分类法类似于类别-组织为树,具有父/子/兄弟分类法,而非层次分类法使用类似于标记的结构。您需要将屏幕截图中显示的所有当前代码替换为以下代码。
(为了将来的参考,提供代码的首选方法是将其作为代码示例,而不是图像-这使其他人更容易帮助您,顺便说一下,也会增加您获得帮助的机会:)
/*
Setup Custom Post Type "store_product"
========================================================================== */
function dm_create_online_store_post_type() {
$labels = array(
\'name\' => \'Products\',
\'singular_name\' => \'Product\',
\'menu_name\' => \'Online Store\',
\'parent_item_colon\' => \'Parent Product:\',
\'all_items\' => \'All Products\',
\'view_item\' => \'View Product\',
\'add_new_item\' => \'Add New Product\',
\'add_new\' => \'Add New\',
\'edit_item\' => \'Edit Product\',
\'update_item\' => \'Update Product\',
\'search_items\' => \'Search Products\',
\'not_found\' => \'Not found\',
\'not_found_in_trash\' => \'Not found in Trash\',
);
$rewrite = array(
\'slug\' => \'product\', // http://your-domain.com/product/product-name/
\'with_front\' => true,
\'pages\' => true, // Allow pagination
\'feeds\' => true,
);
$args = array(
\'label\' => \'store_product\',
\'description\' => \'Online Store\',
\'labels\' => $labels, // This just pulls from the previous array of labels
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', ), // Fields available on the add/edit screen
\'taxonomies\' => array( \'product_category\' ), // Referencing the taxonomy, created in next section, to replace the default categories
\'hierarchical\' => true, // If true, structure would be like pages. If false, it\'s like posts.
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true, // This tells it to show up in your admin menu
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5, // Position in admin menu
\'menu_icon\' => \'http://\', // Change this to the location of your icon
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'post\',
);
register_post_type( \'store_product\', $args ); // Now that everything is setup, register it.
}
add_action( \'init\', \'dm_create_online_store_post_type\', 0 ); // Necessary to actually run the above function
/*
Setup Custom Taxonomy "product_category" for use with above post type
========================================================================== */
function dm_create_online_store_product_categories() {
$labels = array(
\'name\' => \'Product Categories\',
\'singular_name\' => \'Product Category\',
\'menu_name\' => \'Product Categories\',
\'all_items\' => \'All Categories\',
\'parent_item\' => \'Parent Category\',
\'parent_item_colon\' => \'Parent Category:\',
\'new_item_name\' => \'New Category Name\',
\'add_new_item\' => \'Add New Category\',
\'edit_item\' => \'Edit Category\',
\'update_item\' => \'Update Category\',
\'separate_items_with_commas\' => \'Separate categories with commas\',
\'search_items\' => \'Search categories\',
\'add_or_remove_items\' => \'Add or remove categories\',
\'choose_from_most_used\' => \'Choose from the most used categories\',
\'not_found\' => \'Not Found\',
);
$rewrite = array(
\'slug\' => \'product-category\', // http://your-domain.com/product-category/product-category-name
\'with_front\' => true,
\'hierarchical\' => true,
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true, // If true, they act like categories. If false, like tags.
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'rewrite\' => $rewrite,
);
register_taxonomy( \'product_category\', array( \'store_product\' ), $args ); // Register it to our post type
}
add_action( \'init\', \'dm_create_online_store_product_categories\', 0 ); // Necessary to actually run the above function
如仪表板屏幕所示,结果如下所示:
查看外观/菜单将显示“产品”和“产品类别”,就像之前显示“流派/作者”一样(如果看不到,请打开右上角的“屏幕选项”选项卡,并确保选中它们)。
要填充它们,只需添加一个产品类别和一个产品-将该产品分配到该类别,如下所示:
回顾仪表板中的菜单屏幕,您应该会看到以下两个列表: