templates - 如何在 Woocommerce 产品页面模板中添加自定义分页?
问题描述
如何在 Woocommerce 产品页面模板中添加自定义分页?
这是我的代码:
但是分页不起作用。
有什么建议吗?
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'paged' => $paged,
'product_cat' => 'trekking,rafting',
'orderby' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
并用于显示分页:
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="cu-pagi">
<?php get_next_posts_link('Older', $loop->max_num_pages) ?>
<?php get_previous_posts_link('Newer', $loop->max_num_pages) ?>
</div>
<?php wp_reset_query(); ?>
</div><!--/.products-->
解决方案
得到了答案:
工作代码:
<div class="cu-pagi">
<?php
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
</div>
推荐阅读
- windows - 如何通过注册表隐藏/禁用特定的应用程序桌面快捷方式?
- c# - asp.net core 中的配置键是否应该不区分大小写?
- javascript - 如何在 Objection.js 的关系映射中过滤 2 个不同值的相同字段
- blazor - Blazor WebAssembly - 页面刷新获取 JSInterop Json 错误
- react-native - 警告:expo-cli 尚未针对 Node.js v16.3.0 进行测试
- sql-server - 更新 SSAS 多维数据集中的缺失数据
- google-sheets - 哪个 VLOOKUP 公式更优化性能?包含多个不需要的列的范围,或仅包含搜索键和单列范围的数组
- google-sheets - ImportHTML (nasdaq.com) 给出:“错误:无法获取 URL。”
- ros - 用于 ROS 的 Universal Robot Driver 问题
- tfs - 从 Azure Dev Ops 执行增强的测试套件导出时加载 dataTFS.WebApi.Exception 时出错