Php 在产品帖子上添加页码
这就是网站。。其中一个页面上有产品 我可以在底部添加分页,但它只是上一页和下一页 我需要它来显示页码 请帮忙 这是开始Php 在产品帖子上添加页码,php,wordpress,woocommerce,pagination,Php,Wordpress,Woocommerce,Pagination,这就是网站。。其中一个页面上有产品 我可以在底部添加分页,但它只是上一页和下一页 我需要它来显示页码 请帮忙 这是开始 <?php $args = array( 'post_type' => 'product', 'stock' => 1, 'product_cat' => 'earrings-ring', 'posts_per_page' => '12','order' => 'DESC', 'paged' => $paged );
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'product_cat' => 'earrings-ring', 'posts_per_page' => '12','order' => 'DESC', 'paged' => $paged );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
这是我的分页代码
<h3>Pages:
<?php previous_posts_link( '« PREV', $loop->max_num_pages) ?>
<?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?>
</h3>
</div>
<?php wp_reset_postdata(); ?>
页面:
Solution-1(归档页面)
首先获取要在Wp_查询参数中传递的当前页
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'stock' => 1,
'product_cat' => 'earrings-ring',
'posts_per_page' => '12',
'order' => 'DESC',
'paged' => $paged
);
//prepare query
new WP_Query( $args );
您应该在wp_reset_postdata()之前调用WordPress分页函数
调用分页函数
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPage
) );
此解决方案适用于自定义页面模板。您可以检查wordpress official,在活动主题
function.php
文件中添加以下代码。注意:如果您有子主题,请在子主题
function.php
文件中添加此代码
if ( ! function_exists( 'number_pagination' ) ) :
function number_pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
在要显示数字分页的位置添加以下代码
<?php number_pagination( $loop ); ?>
</div>
我试过了,但是没有显示分页。。。?您是使用归档页面还是自定义模板列出所有产品?我认为这是一个自定义模板。另一个开发人员对网站进行了编码。@Christine我也为自定义模板添加了另一个解决方案。我已经为自定义模板使用了相同的函数。
if ( ! function_exists( 'number_pagination' ) ) :
function number_pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
<?php number_pagination( $loop ); ?>
</div>