簡體   English   中英

如何從 Archive.php WordPress 中排除類別

[英]How to exclude categories from Archive.php WordPress

我使用此代碼創建了一個 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 頁面和存檔中排除更多類別? 我的代碼有什么問題? 或者只顯示一個特定的類別也會很好。

將此代碼添加到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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM