猿问

在产品帖子上添加分页号码

所以这是网站.. https://chicafriqfashion.com/和像https://chicafriqfashion.com/fabrics/这样的页面之一在页面上有产品。


我能够在底部添加分页,但它只是上一页和下一页。


我需要它来显示页码。


请帮忙。


这是开始


<?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( '&laquo; PREV', $loop->max_num_pages) ?>

            <?php next_posts_link( 'NEXT &raquo;', $loop->max_num_pages) ?>

 </h3>

</div>

<?php wp_reset_postdata(); ?>


大话西游666
浏览 164回答 2
2回答

慕桂英4014372

解决方案-1(存档页面)首先获取要传入 Wp_Query 参数的当前页面。&nbsp; &nbsp; &nbsp; &nbsp; // current page&nbsp; &nbsp; &nbsp; &nbsp; $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;&nbsp; &nbsp; &nbsp; &nbsp; // prepare arguments&nbsp; &nbsp; &nbsp; &nbsp; $args = array( 'post_type' => 'product',&nbsp; &nbsp; &nbsp; &nbsp;'stock' => 1,&nbsp; &nbsp; &nbsp; &nbsp;'product_cat' => 'earrings-ring',&nbsp; &nbsp; &nbsp; &nbsp;'posts_per_page' => '12',&nbsp; &nbsp; &nbsp; &nbsp;'order' => 'DESC',&nbsp; &nbsp; &nbsp; &nbsp;'paged' => $paged&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; //prepare query&nbsp; &nbsp; &nbsp; &nbsp; new WP_Query( $args );&nbsp;您应该在 wp_reset_postdata() 之前调用 WordPress 分页函数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the_posts_pagination( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'prev_text'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => '<span class="fa fa-angle-left" aria-hidden="true"></span>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'next_text'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => '<span class="fa fa-angle-right" aria-hidden="true"></span>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'screen_reader_text' => '&nbsp;',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'before_page_number' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mid_size'&nbsp; &nbsp; => 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) );解决方案-2(自定义模板)构造 Wp 查询&nbsp; &nbsp; &nbsp; &nbsp; // prepare arguments&nbsp; &nbsp; &nbsp; &nbsp; $args = array( 'post_type' => 'product',&nbsp; &nbsp; &nbsp; &nbsp;'stock' => 1,&nbsp; &nbsp; &nbsp; &nbsp;'product_cat' => 'earrings-ring',&nbsp; &nbsp; &nbsp; &nbsp;'posts_per_page' => '12',&nbsp; &nbsp; &nbsp; &nbsp;'order' => 'DESC',&nbsp; &nbsp; &nbsp; &nbsp;'paged' => $paged&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; &nbsp; &nbsp; //prepare query&nbsp; &nbsp; &nbsp; &nbsp; $query = new WP_Query( $args );&nbsp; &nbsp; &nbsp; &nbsp; $totalPage=$query->max_num_pages;调用分页功能&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $big = 999999999; // need an unlikely integer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo paginate_links( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'format' => '?paged=%#%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'current' => max( 1, get_query_var('paged') ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'total' => $totalPage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) );此解决方案适用于自定义页面模板。您可以查看 wordpress 官方文档。

Smart猫小萌

在活动主题function.php文件中添加以下代码。注意:如果您有子主题,请在子主题function.php文件中添加此代码if ( ! function_exists( 'number_pagination' ) ) :&nbsp; &nbsp; function number_pagination() {&nbsp; &nbsp; &nbsp; &nbsp; global $wp_query;&nbsp; &nbsp; &nbsp; &nbsp; $big = 999999999;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo paginate_links( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'format' => '?paged=%#%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'current' => max( 1, get_query_var('paged') ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'total' => $wp_query->max_num_pages&nbsp; &nbsp; &nbsp; &nbsp; ) );&nbsp; &nbsp; }endif;在要显示数字分页的位置添加以下代码。<?php number_pagination( $loop ); ?></div>
随时随地看视频慕课网APP
我要回答