你能告诉我我的代码有什么问题吗?我的代码是按类别显示自定义帖子。我的代码运行得很好。但是,当我放置一个分页时,代码现在是错误的。
<?php
$ourteam_category_check = '4';
$paged = (int) get_query_var('paged'); --> I inserted this
$niche_ourteam_args = array(
'posts_per_page' => 10,
'paged' => $paged, --> I inserted this
'post_type' => 'shop',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
'tax_query' => array(
array(
'taxonomy' => 'shop_cat',
'field' => 'term_id',
'terms' => $ourteam_category_check
),
),
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
?> -->在此处回显类别4中的所有项目
<?php endwhile; endif; ?>
<div class="page-nav-area">
<?php
if( function_exists('wp_pagenavi') ) {
wp_pagenavi(array('query' => $the_query));
}
?>
</div> 发布于 2021-08-09 05:51:08
试试看,这段代码在我这边运行得很好
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'shop',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID, // your categories or you can use 'category__in' => array(2,6)
),
),
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>https://stackoverflow.com/questions/68707052
复制相似问题