[英]How to exclude categories from Archive.php WordPress
I created a blognews.php page (it shows the last news with a monthly archive) with this code to exclude some categories.我使用此代码创建了一个 blognews.php 页面(它显示带有每月存档的最新新闻)以排除某些类别。
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => 54,
'category__not_in' => 3 ,
'order' => 'DESC'
);
$query = new WP_Query($args);
and it works, the articles under this category aren't displayed.它有效,此类别下的文章不显示。 But when I do the same thing to archive.php and I click a month, I see the posts under the excluded category.
但是当我对 archive.php 做同样的事情并单击一个月时,我会看到排除类别下的帖子。 I tried this but it doesn't work:
我试过这个,但它不起作用:
<?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' );
Is there an easy way to exclude more categories from the blognews.php page and the archive?有没有一种简单的方法可以从 blognews.php 页面和存档中排除更多类别? What's wrong with my code?
我的代码有什么问题? Or show only a specific category it will be good too.
或者只显示一个特定的类别也会很好。
Add this code to the functions.php file将此代码添加到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' );
Here all conditions where is_archive is true:这里是 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.