我在archive.php页面中设置了WP_Query。这样我就可以做不同的事情,例如分页等。我还有两个下拉过滤器类别和存档(每年)。
选择年份时,我将转到具有以下URL www.mysite.com/2018/的页面,分页也可以访问www.mysite.com/2018/page/2/。但是,当按年份筛选网站时,其显示的是所有年份而不是所选年份的帖子。
这是我的WP_query代码:
<?php
$currentCategory = single_cat_title("", false);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => $currentCategory,
'paged'=> $paged,
'posts_per_page' => '9'
);
$query = new WP_Query( $args );
$tax = 'category';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="row">
<div class="col-md-12">
<?php if (is_date()) {?>
<h1 class="pagetitle"><?php the_time('Y'); ?></h1>
<?php } else {?>
<h1 class=""><?php echo $currentCategory?></h1>
<?php } ?>
</div>
</div>
<div class="clearfix">
<div class="select-filter-wrapper float-left">
<div class="clearfix">
<p class="p-filter float-left">Categories:</p>
<div id="category-filter" class="current-filter float-left">
<div id="category-select" class="select-option">
<?php if (is_date()) {?>
<div id="category-select" class="select-option"><p>All</p></div>
<?php } else {?>
<p><?php echo $currentCategory?></p>
<?php } ?>
</div>
<ul id="category-list" class="select-dropdown">
<li class="cat-item cat-item-all">
<a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">All</a>
</li>
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$currentTerm = $term->term_taxonomy_id;
wp_list_categories( array(
'orderby' => 'description',
'order' => 'ASC',
'style' => 'list',
'title_li' => __( '' ),
'taxonomy' => 'category',
'walker' => null,
'current_category' => ( isset($currentTerm) ) ? $currentTerm : null
));
?>
</ul>
</div>
</div>
</div>
<div class="select-filter-wrapper float-left">
<div class="clearfix">
<p class="p-filter float-left">Archive:</p>
<div id="category-filter" class="current-filter float-left">
<div id="category-select" class="select-option">
<?php if (is_date()) {?>
<p><?php the_time('Y'); ?></p>
<?php } else {?>
<p>Year</p>
<?php } ?>
</div>
<ul id="category-list" class="select-dropdown">
<!--<li class="current-cat"><a href="<//?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">All</a></li>-->
<?php $args = array(
'type' => 'yearly',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => 'post'
);
wp_get_archives( $args );
?>
</ul>
</div>
</div>
</div>
</div>
<?php endif;
if ( $query->have_posts() ): ?>
<div class="row row-eq-height">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-md-4 o-mg-btm-md">
<?php get_template_part( 'template-parts/content', get_post_type() );?>
</div>
<?php endwhile; ?>
</div>
<?php wordpress_numeric_post_nav(); ?>
<?php endif; ?>
我是否需要在$ args中添加其他内容才能使年度过滤正常工作?