繁体   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