简体   繁体   中英

How to reset the page, go back to first page on wordpress ( wp_query,paginate_links)?

I use the follow code to handle WordPress pagination. For instance, if the user goes to page 9, My code will echo this URL ' http://jjh.com/Cute/page/9/?post_per_page=60 '.

My problem is this: can I reset the page to 1 whenever I change the post_per_page number? I allow the user to change this parameter, and I want to reset to the top when they use it.

 <?php //I cut some unnecessary code // I have a select dropdown to select post_per_page $arg = array( 'post_type' => 'post', 'posts_per_page' => $post_per_page, 'cat' => $page_cat, 'paged' => $paged, 'offset' => $offset, 'prev_text' => __('«'), 'next_text' => __('»'), 'mid_size' => 2 ); $wp_query = new WP_Query($arg); echo paginate_links( $arg ); ?> 

Try this code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $post_type = 'post';
                $args=array(
                   'post_typr' => $post_type,
                    'cat' =>3,
                    'posts_per_page' => 6,
                    'paged'          => $paged,
                    'orderby' => 'title',
                    'order' => 'asc'

                );
                $my_query = null;
                $my_query = new WP_Query($args);

After adding this code use while query <?php if ($my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php if ($my_query->have_posts() ) : while ($my_query->have_posts()) : $my_query->the_post(); ?>

After while query end tag use <?php $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $my_query->max_num_pages ) ); ?> <?php $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $my_query->max_num_pages ) ); ?>

And then close if statement. <?php endif; wp_reset_query();?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM