3

我遇到了 WordPress 的有线问题。下面是我的事件帖子类型的代码,它在没有功能的情况下工作,但是当添加功能时,所有默认角色(管理员、编辑器等)都不能使用帖子类型。管理员角色只能查看自定义分类。

对于能够提交事件以供审核的用户角色,我有一个带有“edit_events => true”的自定义用户角色。这是我想要的,但是内置角色看不到帖子类型!

我已经尝试了几乎所有我能找到的功能教程,它们似乎都几乎相同,我不知道它们与我的代码有什么不同。

function register_custom_event_type() {
$labels = array(
    'name' => _x('Events', 'event'),
    'singular_name' => _x('Event', 'event'),
    'add_new' => _x('Add New', 'event'),
    'add_new_item' => _x('Add New Event', 'event'),
    'edit_item' => _x('Edit Event', 'event'),
    'new_item' => _x('New Event', 'event'),
    'view_item' => _x('View Event', 'event'),
    'search_items' => _x('Search Events', 'event'),
    'not_found' => _x('No events found', 'event'),
    'not_found_in_trash' => _x('No events found in Trash', 'event'),
    'parent_item_colon' => _x('Parent Event:', 'event'),
    'menu_name' => _x('Events', 'event'),
);

$args = array(
    'labels' => $labels,
    'hierarchical' => false,
    'supports' => array('title', 'editor', 'thumbnail', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'event',
    'capabilities' => array(
            'read_post' => 'read_event',
            'publish_posts' => 'publish_events',
            'edit_posts' => 'edit_events',
            'edit_others_posts' => 'edit_others_events',
            'delete_posts' => 'delete_events',
            'delete_others_posts' => 'delete_others_events',
            'read_private_posts' => 'read_private_events',
            'edit_post' => 'edit_event',
            'delete_post' => 'delete_event',

        ),
    'map_meta_cap' => true
);
register_post_type('event', $args);
}
add_action('init', 'register_custom_event_type');
4

5 回答 5

9

最后我找到了解决办法。

我认为默认的 WordPress 角色对帖子类型具有与普通帖子相同的功能,但由于某种原因,它们没有。

手动添加功能似乎有效。

function add_event_caps() {
  $role = get_role( 'administrator' );
  $role->add_cap( 'edit_event' ); 
  $role->add_cap( 'edit_events' ); 
  $role->add_cap( 'edit_others_events' ); 
  $role->add_cap( 'publish_events' ); 
  $role->add_cap( 'read_event' ); 
  $role->add_cap( 'read_private_events' ); 
  $role->add_cap( 'delete_event' ); 
  $role->add_cap( 'edit_published_events' );   //added
  $role->add_cap( 'delete_published_events' ); //added
}
add_action( 'admin_init', 'add_event_caps');
于 2013-08-20T18:44:53.120 回答
5

确保 'map_meta_cap' => true,在注册您的自定义帖子类型时包括正确设置功能类型,例如。'capability_type' => array('event', 'events'),然后使用用户角色编辑器插件并单击您认为适合各个角色的权限 - 完成!

于 2017-04-07T09:50:31.947 回答
1

设置'map_meta_cap' => true和设置后,'capability_type' => array('event', 'events')您可能还需要添加功能:

除了 map_meta_cap() 之外,还有八种其他基本功能没有在核心中直接引用,它采用上述三个元功能并将它们转换为一个或多个基本功能,然后必须根据用户或角色检查这些基本功能上下文。这些附加功能仅在 map_meta_cap() 中使用。因此,只有在帖子类型注册时将“map_meta_cap”参数设置为 true(默认为 false),才会默认分配它们。

read - Controls whether objects of this post type can be read.
delete_posts - Controls whether objects of this post type can be deleted.
delete_private_posts - Controls whether private objects can be deleted.
delete_published_posts - Controls whether published objects can be deleted.
delete_others_posts - Controls whether objects owned by other users can be can be deleted. If the post type does not support an author, then this will behave like delete_posts.
edit_private_posts - Controls whether private objects can be edited.
edit_published_posts - Controls whether published objects can be edited.
create_posts - Controls whether new objects can be created

register_post_type() Wordpress 法典

于 2018-07-02T11:12:22.853 回答
0

那是一个奇怪的问题。它应该工作。以下内容对您有用(请注意,我只是复制粘贴了我的代码,所以我的一些代码潜入了)

add_action('init', 'register_custom_event_type');
function register_custom_event_type() {
  register_post_type('event',
    array(
    'labels' => array(
      'name' => _x('Event', 'Event'),
      'singular_name' => _x('Event Custom Post', 'domain'),
      'add_new' => _x('Add New', 'domain'),
      'add_new_item' => _x('Add New Event Custom Post', 'domain'),
      'edit' => _x('Edit', 'domain'),
      'edit_item' => _x('Edit Event Custom Post', 'domain'),
      'new_item' => _x('New Event Custom Post', 'domain'),
      'view' => _x('View Event Custom Post', 'domain'),
      'view_item' => _x('View Event Custom Post', 'domain'),
      'search_items' => _x('Search Event Custom Post', 'domain'),
      'not_found' => _x('No Event Custom Posts found', 'domain'),
      'not_found_in_trash' => _x('No Event Custom Posts found in Trash', 'domain')
    ),
    'public' => true,
    'hierarchical' => true,
    'has_archive' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
    'can_export' => true,
    'taxonomies' => array( 'category' ),
    'register_meta_box_cb' => '',
    'map_meta_cap' => true,
    'capability_type' => 'event',
    'capabilities' => array(
      'read_post' => 'read_event',
      'publish_posts' => 'publish_events',
      'edit_posts' => 'edit_events',
      'edit_others_posts' => 'edit_others_events',
      'delete_posts' => 'delete_events',
      'delete_others_posts' => 'delete_others_events',
      'read_private_posts' => 'read_private_events',
      'edit_post' => 'edit_event',
      'delete_post' => 'delete_event',
     ),
  ));
}

与您称呼它的方式有关。不知道在哪里。有时重新开始会更容易:)

祝你好运

于 2013-08-20T08:44:02.697 回答
-2

注册帖子类型时,添加支持标签启用编辑器,如下所示: 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' )

于 2015-04-16T14:40:27.813 回答