繁体   English   中英

为什么我的WordPress类别帖子未在其类别页面上列出?

[英]Why aren't my WordPress category posts listing on their category pages?

我在这个问题上的脚步太久了,我已经没时间了。 因此,我转向您,堆栈用户。

背景知识:根据您选择查看网站的方式,该网站省略了某些类别。 初中/高中用户看到某些类别和帖子,成年Ed用户看到MS / HS学生没有的帖子和类别。 此功能的第一部分起作用。 其余的没有,我不知道为什么。 该功能的第一部分过滤掉必要的帖子,包括事件和视频。 如果您位于各自的类别页面上,则最后两个条件将还原它们。 或者,应该。 那是意图。 不是。

function filter_posts( $query ) {
if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
    if ( isset( $_COOKIE['branch'] ) ) :
        $postsToExclude = array('15','10','16');
        if ( $_COOKIE['branch'] == 'middle-high-school' ) :
            array_push($postsToExclude, '69','36','70');
        elseif ( $_COOKIE['branch'] == 'adult-education' ) :
            array_push($postsToExclude, '35', '40', '68', '36','70');
        endif;

        $query->set( 'category__not_in', $postsToExclude );
    endif;
endif;

if ( $query->is_front_page() && $query->is_main_query() ) :
    // We also need to exclude the featured post from the main blog feed.
    $custom_meta[] = array(
        'key' => 'blog_feature_this',
        'value' => '"Yes"',
        'compare' => 'NOT IN'
    );
    $query->set('meta_query', $custom_meta);
endif;

if ( is_category( 'events' ) && $query->is_main_query() ) :
    $query->set( 'category__in', array(10) );
endif;

if ( is_category( 'videos' ) && $query->is_main_query() ) :
    $query->set( 'category__in', array(16) );
endif;
}
add_action( 'pre_get_posts', 'filter_posts' );

请尝试类似这样的操作-仅使用一个参数并根据需要调整值的数组。

function filter_posts( $query ) {

  // Define the array
  $postsToExclude = array();

  if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
      if ( isset( $_COOKIE['branch'] ) ) :
          $postsToExclude = array('15','10','16');
          if ( $_COOKIE['branch'] == 'middle-high-school' ) :
              array_push($postsToExclude, '69','36','70');
          elseif ( $_COOKIE['branch'] == 'adult-education' ) :
              array_push($postsToExclude, '35', '40', '68', '36','70');
          endif;
      endif;
  endif;

  if ( $query->is_front_page() && $query->is_main_query() ) :
      // We also need to exclude the featured post from the main blog feed.
      $custom_meta[] = array(
          'key' => 'blog_feature_this',
          'value' => '"Yes"',
          'compare' => 'NOT IN'
      );
      $query->set('meta_query', $custom_meta);
  endif;

  if ( is_category( 'events' ) && $query->is_main_query() ) :
    // If the category is in the exclude list remove it
    if(array_key_exists('10',$postsToExclude)) {
      unset($postsToExclude['10'];
    }
  endif;

  if ( is_category( 'videos' ) && $query->is_main_query() ) :
    // If the category is in the exclude list remove it
    if(array_key_exists('10',$postsToExclude)) {
      unset($postsToExclude['16'];
    }
  endif;

  // If there are some values
  if(!empty($postsToExclude)) {
    $query->set( 'category__not_in', $postsToExclude );
  }

}
add_action( 'pre_get_posts', 'filter_posts' );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM