当您使用register_post_type
函数,在第二个参数中($args
) 您可以更改一些参数。
有关更多信息,请参阅法典:https://codex.wordpress.org/Function_Reference/register_post_type#Parameters
良好的链接还包括:https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table 获取有关默认功能和角色的更多信息
以下是“book”自定义帖子类型文档中的一个示例(将此添加到$args
)
\'capabilities\' => array(
\'edit_post\' => \'edit_book\',
\'read_post\' => \'read_book\',
\'delete_post\' => \'delete_book\',
\'edit_posts\' => \'edit_books\',
\'edit_others_posts\' => \'edit_others_books\',
\'publish_posts\' => \'publish_books\',
\'read_private_posts\' => \'read_private_books\',
\'create_posts\' => \'edit_books\',
),
您还必须在参数中添加“map\\u meta\\u cap”:
\'map_meta_cap\' => true
有了它,您可以为自定义帖子类型使用映射设置“新”功能
function wpse_288266_add_theme_caps() {
// Add cap for the admins
$admins = get_role(\'administrator\');
$admins->add_cap(\'edit_book\');
$admins->add_cap(\'edit_books\');
$admins->add_cap(\'edit_other_books\');
$admins->add_cap(\'publish_books\');
$admins->add_cap(\'read_book\');
$admins->add_cap(\'read_private_books\');
$admins->add_cap(\'delete_book\');
$contribs = get_role(\'contributor\');
$contribs->add_cap(\'edit_book\');
$contribs->add_cap(\'edit_books\');
$contribs->add_cap(\'edit_other_books\');
$contribs->add_cap(\'publish_books\');
$contribs->add_cap(\'read_book\');
$contribs->add_cap(\'read_private_books\');
$contribs->add_cap(\'delete_book\');
// Here you remove the rights for other post type without the capabilities mapping
$contribs->remove_cap(\'edit_post\');
$contribs->remove_cap(\'edit_posts\');
$contribs->remove_cap(\'edit_other_posts\');
$contribs->remove_cap(\'publish_posts\');
$contribs->remove_cap(\'delete_post\');
/* You\'ll probably want to let them read the posts
$contribs->remove_cap(\'read_post\');
$contribs->remove_cap(\'read_private_posts\');
*/
}
add_action(\'admin_init\', \'wpse_288266_add_theme_caps\');
为了回答在管理栏中隐藏一些按钮的问题,我将在body类中添加角色并添加一些CSS
function wpse_288266_add_role_body_class($classes) {
// Get the current user (the action happend when logged in)
$current_user = new WP_User(get_current_user_id());
$role = array_shift($current_user->roles);
if(is_admin())
{
$classes .= \'user-role-\'. $role;
}
else
{
$classes[] = \'user-role-\'. $role;
}
return $classes;
}
function wpse_288266_add_role_style() {
?>
<style type="text/css" media="screen">
body.user-role-contibutor #wpadminbar #wp-admin-bar-comments {
display: none;
}
body.user-role-contibutor.single-YOUR_POST_TYPE_KEY #wpadminbar #wp-admin-bar-comments {
display: list-item;
}
</style>
<?php
}
// We apply our code only for logged-in users
if(is_user_logged_in())
{
add_filter(\'body_class\',\'wpse_288266_add_role_body_class\');
add_filter(\'admin_body_class\',\'wpse_288266_add_role_body_class\');
add_action(\'wp_footer\', \'wpse_288266_add_role_style\'); // You can do better directly in your real stylesheet
add_action(\'admin_footer\', \'wpse_288266_add_role_style\');
}
实际上,我没有“干净”的想法来删除为自定义帖子类型发表评论的功能:/对不起,我的英语太差了!