对于我的自定义帖子类型之一,我希望特定用户能够编辑管理员创建的现有帖子,但不能添加新帖子。
如何做到这一点?
如果我将用户角色定义为无法发布,则仍然允许他们添加新帖子并提交以供审阅。
对于我的自定义帖子类型之一,我希望特定用户能够编辑管理员创建的现有帖子,但不能添加新帖子。
如何做到这一点?
如果我将用户角色定义为无法发布,则仍然允许他们添加新帖子并提交以供审阅。
您必须这样做:
function hide_buttons() {
global $current_screen;
if($current_screen->id == \'edit-post\' && !current_user_can(\'publish_posts\')) {
echo \'<style>.add-new-h2{display: none;}</style>\';
}
}
add_action(\'admin_head\',\'hide_buttons\');
请参见:http://erisds.co.uk/wordpress/spotlight-wordpress-admin-menu-remove-add-new-pages-or-posts-link 供参考前面的答案只是用CSS隐藏菜单项,正如@ezejielDFM指出的那样,它不会阻止用户添加帖子。
相反,注册自定义帖子类型时,需要设置create_posts
值到do_not_allow
(或false
在4.5以下的Wordpress版本中),并将map_meta_cap
到true
.
register_post_type( \'custom_post_type_name\', array(
\'capability_type\' => \'post\',
\'capabilities\' => array(
\'create_posts\' => \'do_not_allow\', // Prior to Wordpress 4.5, this was false
),
\'map_meta_cap\' => true, // With this set to true, users will still be able to edit & delete posts
));
如果map_meta_cap
被忽略,默认为false
尽管你已经禁用了Add New 您也无法编辑或删除现有的帖子,因此请确保包含该值。全部学分归this answer 堆栈溢出时。