如果我知道你有一些类别,例如:“保留”,“人”,“风景”,“个人”等等。
现在,您希望订阅者可以看到“人物”、“风景”类别中的帖子,但不能看到“保留”和“个人”类别中的帖子。
这相对容易,只需pre_get_posts 如果请求的帖子包含该术语或术语存档,则禁用查看。
要禁用不同的通道,可以重定向、显示404或显示自定义模板。
下面我要说的是如何保持最后的选择。
首先创建一个模板文件,例如。not-allowed.php 并放入主题文件夹。
然后在functions.php 使用此代码:
add_filter(\'template_include\', \'restict_by_category\');
function check_user() {
  $user = wp_get_current_user();
  if ( ! $user->ID || in_array(\'subscriber\', $user->roles) ) {
    // user is not logged or is a subscriber
    return false;
  }
  return true;
}
function restict_by_category( $template ) {
  if ( ! is_main_query() ) return $template; // only affect main query.
  $allow = true;
  $private_categories = array(\'reserved\', \'personal\'); // categories subscribers cannot see
  if ( is_single() ) {
    $cats = wp_get_object_terms( get_queried_object()->ID, \'category\', array(\'fields\' => \'slugs\') ); // get the categories associated to the required post
    if ( array_intersect( $private_categories, $cats ) ) {
      // post has a reserved category, let\'s check user
      $allow = check_user();
    }
  } elseif ( is_tax(\'category\', $private_categories) ) {
    // the archive for one of private categories is required, let\'s check user
    $allow = check_user();
  }
  // if allowed include the required template, otherwise include the \'not-allowed\' one
  return $allow ? $template : get_template_directory() . \'/not-allowed.php\';
}