我一直在尝试修复分类页面的分页,但我不知所措。这是我的pre\\u get\\u posts功能,用于分类页面。
add_action( \'pre_get_posts\', function ( $q )
{
if ( !is_admin() // Only target the front end queries
&& $q->is_main_query() // Targets the main query only
&& $q->is_category() // Only target category pages
) {
$today = date( \'Y-m-d\' );
$q->set( \'post_type\', array( \'research_article\', \'events\' ) );
$q->set( \'posts_per_page\', 12 );
$q->set( \'orderby\', \'meta_value_num date\' );
$q->set( \'meta_query\', array(
array(
\'key\' => \'wpcf-date_time\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'DATE\',
),
array(
\'key\' => \'wpcf-date_time\',
\'compare\' => \'NOT EXISTS\'
),
\'relation\' => \'OR\',
) );
}
});
除了每页不显示12篇文章或不包含分页之外,所有内容都正常运行。我试过包括
the_posts_pagination() 在我的类别上。php没有运气。我应该在这个pre\\u get\\u posts函数中设置分页变量吗?如果是,如何?
我的类别。php代码:
<?php get_header(); ?>
<div class="welcome">
<h1><?php echo single_cat_title(); ?></h1>
<?php echo category_description(); ?>
</div>
<div class="site">
<main id="primary" class="site-main two-col-index">
<?php // Output custom query loop
if ( have_posts() ) :
$i = 0;
while ( have_posts() ) :
the_post();
if ( $i == 0 ) : ?>
<div class="post-box">
....
</div> <!-- closes the first div box -->
<?php else: ?>
<?php get_template_part( \'template-parts/content\', \'post_box\' ); ?>
<?php endif; $i++; ?>
<?php
endwhile;
endif;
the_posts_pagination();
?>
</main><!-- #main -->
</div>
<?php get_footer(); ?>