在您已经输出HTML之后,WP无法重定向-因此,在调用标题之前,将if/else移到顶部,并且只输出标题(&L);如果满足条件,请填写页脚。
另外,不使用current_user_can(\'role\'), 使用current_user_can(\'capability\') - i、 e。
<?php if(
current_user_can(\'activate_plugins\') ||
current_user_can(\'edit_a_defined_custom_post_type\')
) {
get_header(); ?>
<div>Restricted content here</div><?php
get_footer();
} else {
wp_redirect(wp_login_url());
} ?>
(根据Codex,“虽然部分支持检查替代能力的特定角色,但不鼓励这种做法,因为它可能会产生不可靠的结果。”)