WooCommerce 产品页面分页显示所有页面中的重复项
WooCommerce product page pagination showing duplicates in all pages
我使用 WooCommerce 分页在每个页面上得到相同的结果。我究竟做错了什么?也许它在某处缺少 postdata 重置?这是我的代码示例:
$products = new WP_Query($product_args);
if ($products->have_posts()):
while ($products->have_posts()):
$products->the_post();
wc_get_template_part("content", "product");
endwhile;
wp_reset_postdata($products);
endif;
echo paginate_links([
"base" => str_replace(
999999999,
"%#%",
esc_url(get_pagenum_link(999999999))
),
"total" => $products->max_num_pages,
"current" => max(1, get_query_var("paged")),
"format" => "?paged=%#%",
"show_all" => false,
"type" => "list",
"end_size" => 2,
"mid_size" => 4,
"prev_next" => true,
"prev_text" => is_rtl() ? "→" : "←",
"next_text" => is_rtl() ? "←" : "→",
"add_args" => false,
"add_fragment" => "",
]);
感谢@Outsource WordPress 的评论,我找到了解决方案。我像这样更改了代码的一部分并使其正常工作。我用 paged:
替换了 offset
$product_args = [
"post_type" => "product",
"post_status" => "publish",
"orderby" => "ID",
"suppress_filters" => false,
"order" => "ASC",
"paged" => max(1, get_query_var('paged')),
"posts_per_page" => "16",
];
我使用 WooCommerce 分页在每个页面上得到相同的结果。我究竟做错了什么?也许它在某处缺少 postdata 重置?这是我的代码示例:
$products = new WP_Query($product_args);
if ($products->have_posts()):
while ($products->have_posts()):
$products->the_post();
wc_get_template_part("content", "product");
endwhile;
wp_reset_postdata($products);
endif;
echo paginate_links([
"base" => str_replace(
999999999,
"%#%",
esc_url(get_pagenum_link(999999999))
),
"total" => $products->max_num_pages,
"current" => max(1, get_query_var("paged")),
"format" => "?paged=%#%",
"show_all" => false,
"type" => "list",
"end_size" => 2,
"mid_size" => 4,
"prev_next" => true,
"prev_text" => is_rtl() ? "→" : "←",
"next_text" => is_rtl() ? "←" : "→",
"add_args" => false,
"add_fragment" => "",
]);
感谢@Outsource WordPress 的评论,我找到了解决方案。我像这样更改了代码的一部分并使其正常工作。我用 paged:
替换了 offset$product_args = [
"post_type" => "product",
"post_status" => "publish",
"orderby" => "ID",
"suppress_filters" => false,
"order" => "ASC",
"paged" => max(1, get_query_var('paged')),
"posts_per_page" => "16",
];