最简单的方法是创建两种自定义帖子类型。一个是“新闻”,另一个是“新内容”。
例如,您可以在/wp-content/plugins/
调用news-post-types.php
. 以下代码将创建仅适用于此帖子类型的特殊权限:
<?php
/* Plugin Name: Post types and permissions for News and What\'s New
*/
// on init, create a custom post type called News
add_action(\'init\', \'wpse_create_post_types\');
function wpse_create_post_types() {
// Capabilities: this is how you\'ll enable some users to edit, delete, etc.
$capabilities = array(
\'edit_post\' => \'edit_news\',
\'read_post\' => \'read_news\',
\'delete_post\' => \'delete_news\',
\'create_posts\' => \'create_news\',
\'delete_posts\' => \'delete_news\',
\'delete_others_posts\' => \'delete_others_news\',
\'delete_private_posts\' => \'delete_private_news\',
\'delete_published_posts\' => \'delete_published_news\',
\'edit_posts\' => \'edit_news\',
\'edit_others_posts\' => \'edit_others_news\',
\'edit_private_posts\' => \'edit_private_news\',
\'edit_published_posts\' => \'edit_published_news\',
\'publish_posts\' => \'publish_news\',
\'read_private_posts\' => \'read_private_news\'
);
// Other CPT arguments
$args = array(
// Important: make sure to include the next 2 args for capabilities
\'map_meta_cap\' => true,
\'capabilities\' => $capabilities,
// You can adjust the other args as needed
\'public\' => true,
\'has_archive\' => true,
\'hierarchical\' => false,
\'supports\' => array(\'title\', \'editor\', \'author\', \'page-attributes\')
);
// Actually create the post type
register_post_type(\'news\', $args);
}
只要您指定
has_archive
注册自定义帖子类型时,也会自动为每个帖子获取一个类似于类别的页面,而无需创建任何自定义模板。
确保激活插件,并访问设置>永久链接页面。您不需要在那里更改任何内容,但这将刷新您的重写规则,以便新URL正常运行。
现在,您有了一个具有自定义功能的自定义post类型。您至少还需要做一件事:创建一个具有这些权限的角色。现在,您的任何用户(甚至管理员)都没有访问权限,这是因为您刚刚创建了不会自动映射到任何用户的自定义功能。
在同一插件文件中:
// \'news_author\' is a slug, \'News Author\' is how you will see this role in wp-admin
add_role(\'news_author\', \'News Author\', array(
// You may or may not want to give them all these capabilities.
// For example you could let them publish but not delete.
\'delete_news\' => true,
\'create_news\' => true,
\'delete_news\' => true,
\'delete_others_news\' => true,
\'delete_private_news\' => true,
\'delete_published_news\' => true,
\'edit_news\' => true,
\'edit_others_news\' => true,
\'edit_private_news\' => true,
\'edit_published_news\' => true,
\'publish_news\' => true,
\'read_private_news\' => true,
// If you want them to be able to upload files and read on the front end,
// make sure to include the following 2 capabilities:
\'read\' => true,
\'upload_files\' => true
));
最后,你可能还想让管理员(也许像你一样)能够添加/编辑/删除这些自定义新闻帖子。如果是,请将以下内容添加到插件:
add_action(\'admin_init\', \'wpse_add_admin_caps\');
function wpse_add_admin_caps() {
$role = get_role(\'administrator\');
$role->add_cap(\'delete_news\');
$role->add_cap(\'create_news\');
$role->add_cap(\'delete_news\');
$role->add_cap(\'delete_others_news\');
$role->add_cap(\'delete_private_news\');
$role->add_cap(\'delete_published_news\');
$role->add_cap(\'edit_news\');
$role->add_cap(\'edit_others_news\');
$role->add_cap(\'edit_private_news\');
$role->add_cap(\'edit_published_news\');
$role->add_cap(\'publish_news\');
$role->add_cap(\'read_private_news\');
}
您可以对“新增内容”部分执行相同的操作—创建第二个自定义帖子类型,然后为其添加角色,然后也可以选择将这些功能添加到管理员角色。