[英]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.