您可以使用页面和自定义帖子类型来处理此问题。
对于每个人都能看到的内容,请使用常规WP核心页面。
对于受限内容,使用映射的自定义功能构建自定义帖子类型。然后,您可以为每个用户分配一个角色,例如可以查看包括分数在内的所有内容的“coach”,可以查看包括分数在内的团队1帖子的“team1”,以及可以查看包括分数在内的团队2帖子的“team2”。
您可以从自定义帖子类型的代码开始,并在站点上构建所需的任何其他选项:
<?php
// Define custom capabilities for this post type.
$capabilities = array(
\'edit_post\' => \'edit_team1\',
\'read_post\' => \'read_team1\',
\'delete_post\' => \'delete_team1\',
\'create_posts\' => \'create_team1\',
\'delete_posts\' => \'delete_team1\',
\'delete_others_posts\' => \'delete_others_team1\',
\'delete_private_posts\' => \'delete_private_team1\',
\'delete_published_posts\' => \'delete_published_team1\',
\'edit_posts\' => \'edit_team1\',
\'edit_others_posts\' => \'edit_others_team1\',
\'edit_private_posts\' => \'edit_private_team1\',
\'edit_published_posts\' => \'edit_published_team1\',
\'publish_posts\' => \'publish_team1\',
\'read_private_posts\' => \'read_private_team1\',
);
$args = array(
// Map Meta Cap ensures your custom capabilities are used.
\'map_meta_cap\' => true,
\'show_in_rest\' => true,
\'public\' => true,
\'supports\' => array( \'title\', \'editor\', \'author\', \'revisions\', \'page-attributes\' ),
\'capabilities\' => $capabilities,
);
register_post_type( \'team1\', $args );
?>
那么你需要一个
read_team1
任何人都可以阅读团队1帖子(包括分数和您保护的其他内容)。
<?php
add_role(\'team_one_player\', \'Team One Player\', array(
// Let them read Team 1 CPTs.
\'read_team1\' => true,
// Also let them read Core content like Pages.
\'read\' => true
)
);
?>
您可能希望授予管理员编辑/删除权限。默认情况下,它们没有访问权限,因此您必须明确授予它:
<?php
$admin = get_role(\'administrator\');
$admin->add_cap(\'create_team_1\');
$admin->add_cap(\'delete_team1\');
$admin->add_cap(\'delete_others_team1\');
$admin->add_cap(\'delete_private_team1\');
$admin->add_cap(\'delete_published_team1\');
$admin->add_cap(\'edit_team1\');
$admin->add_cap(\'edit_others_team1\');
$admin->add_cap(\'edit_private_team1\');
$admin->add_cap(\'edit_published_team1\');
$admin->add_cap(\'publish_team1\');
$admin->add_cap(\'read_team1\');
$admin->add_cap(\'read_private_team1\');
?>
您只需要仔细考虑如何以对访问者有意义的方式构造URL,还需要考虑UX。例如,您是否打算让站点导航链接到所有页面,并且让没有适当功能的访问者收到“抱歉,您无法访问该页面”消息?或者,是否要构建条件导航,以检查每个链接并仅显示当前用户能够查看的内容?
或者,您可以查看现有的成员插件。您可以为第1组受保护的内容设置“成员身份”,为第2组受保护的内容设置单独类型的成员身份,可能无需编码。