我使用此代碼創建了一個 blognews.php 頁面(它顯示帶有每月存檔的最新新聞)以排除某些類別。
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => 54,
'category__not_in' => 3 ,
'order' => 'DESC'
);
$query = new WP_Query($args);
它有效,此類別下的文章不顯示。但是當我對 archive.php 執行相同的操作并單擊一個月時,我會看到排除類別下的帖子。我試過這個,但它不起作用:
<?php if ( have_posts() ) : ?>
<?php
query_posts( array( 'category__and' => array(54), 'posts_per_page' => 20, 'orderby' => 'title', 'order' => 'DESC' ) );
while ($query->have_posts()):
$query->the_post();
get_template_part( 'template-parts/content', 'excerpt' );
有沒有一種簡單的方法可以從 blognews.php 頁面和存檔中排除更多類別?我的代碼有什么問題?或者只顯示一個特定的類別也會很好。
uj5u.com熱心網友回復:
將此代碼添加到functions.php檔案中
function exclude_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() ) {
$query->set( 'cat', '-3' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
這里是 is_archive 為真的所有條件:
if ( $this->is_post_type_archive
$this->is_date
$this->is_author
$this->is_category
$this->is_tag
$this->is_tax )
$this->is_archive = true;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/336511.html
標籤:php WordPress的
