php - 为什么我的自定义帖子类型仅针对我的自定义用户角色显示?
问题描述
我为我的餐厅创建了自定义帖子类型。它目前仅针对我的 restaurant_owner 自定义用户角色显示。我希望它也显示给管理员。
我究竟做错了什么?
自定义帖子类型:
add_action( 'init', 'pt_restaurant');
function pt_restaurant() {
register_post_type( 'bounty_product', array(
'labels' => array(
'name' => 'Restaurants',
'singular_name' => 'Restaurant',
'add_new' => 'Add New',
'add_new_item' => 'Add New Restaurant',
'edit' => 'Edit',
'edit_item' => 'Edit Restaurant',
'new_item' => 'New Restaurant',
'view' => 'View',
'view_item' => 'View Restaurant',
'search_items' => 'Search Restaurants',
'not_found' => 'No Restaurants found',
'not_found_in_trash' => 'No Restaurants found in Trash',
'parent' => 'Parent Restaurant'),
'description' => 'Used for the Restaurant section',
'public' => true,
'capability_type' => array('bounty_product','bounty_products'),
'map_meta_cap' => true,
'show_in_menu' => true,
'menu_position' => 20,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
));
自定义用户角色:
add_action('init', 'restaurant_owner_user_role');
function restaurant_owner_user_role() {
add_role('restaurant_owner', 'Restaurant Owner',
array (
'edit_bounty_product' => true,
'delete_bounty_product' => false,
'read_bounty_product' => true,
'publish_bounty_products' => false,
'edit_bounty_products' => true,
'edit_others_bounty_products' => false,
'delete_bounty_products' => false,
'delete_others_bounty_products' => false,
'read_private_bounty_products' => false,
'read' => true,
)
);
}
解决方案
您只是为您的自定义用户角色提供功能,这就是管理员无法编辑/更新/删除您的 CPT 的原因。尝试在functions.php中使用以下代码
function add_theme_caps() {
// gets the administrator role
$admins = get_role( 'administrator' );
$admins->add_cap( 'edit_bounty_product' );
$admins->add_cap( 'delete_bounty_product' );
$admins->add_cap( 'read_bounty_product' );
$admins->add_cap( 'publish_bounty_products' );
$admins->add_cap( 'edit_bounty_products' );
$admins->add_cap( 'edit_others_bounty_products' );
$admins->add_cap( 'delete_bounty_products' );
$admins->add_cap( 'delete_others_bounty_products' );
$admins->add_cap( 'read_private_bounty_products' );
$admins->add_cap( 'delete_gallery' );
}
add_action( 'init', 'add_theme_caps');
推荐阅读
- javascript - 如何在 bot 框架 sdk (javascript) 中使用 Luis 创建选择列表
- flutter - 应用程序关闭时颤动通知(应用程序未运行)
- javascript - 你知道为什么我得到错误 $ 没有定义吗?
- javascript - 为什么当我将禁用更改为只读时,只有一个输入字段无法修改。其他输入字段仍然可用
- reactjs - 属性名称必须是“字符串”、“数字”、“符号”或“任意”类型
- openshift - Openshift - 使用服务帐户登录 docker 注册表
- c# - 在 C# 中动态展平 JSON
- javascript - 在函数内部赋值时变量值不会改变
- batch-file - 为什么批处理文件中的文件夹没有正确解释
- flutter - 设置 LiveQuery 以使用 Flutter 时出现问题