简体   繁体   中英

Custom wp_query with working pagination on a page template

I'm trying to add a custom query to a WordPress template and include pagination but my pagination isn't appearing, for example's sake I'm trying to add this to page.php.

I have the following markup which works perfectly when place inside a category template like category.php, the pagination shows up and functions just fine. The issue is that the pagination doesn't appear when the same code is place in page.php or any custom page template.

The query:

              <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $loop = new WP_Query(
                    array(
                        'post_type' => 'post',
                        'posts_per_page' => 10,     
                        'paged' => $paged,
                        'orderby' => 'desc',
                    )
                );
              ?>

              <?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>

                // Loop Markup goes here.

              <?php endwhile; ?> 
              <?php endif; ?>

              <?php wp_reset_query(); ?>

              <?php pagination(); ?>

Pagination() as defined in functions.php:

              function pagination() {
                  global $wp_query;
                  $big = 999999999;
                  echo paginate_links(array(
                      'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                      'format' => '?paged=%#%',
                      'current' => max(1, get_query_var('paged')),
                      'total' => $wp_query->max_num_pages
                  ));
              }
              add_action('init', 'pagination');

I've seen a few posts requesting help on the same subject, but I didn't come across an elegant solution.

Any advice would be much appreciated! My knowledge of PHP is pretty limited, I took the pagination() function from the HTML5 Blank theme by Todd Motto so I don't 100% understand what that function defines.

Managed to find a solution by merging my code from the original post with the following code from this tutorial :

I'm posting a complete example of a simple page.php for anyone lost and needing more context to implement this, this works perfectly for me with no broken aspects like each page number returning the same posts or anything.

<?php get_header(); ?>

    <?php if (have_posts()): while (have_posts()) : the_post(); ?>

            <div class="the_loop">

              <?php        
                if ( get_query_var('paged') ) {
                    $paged = get_query_var('paged');
                } elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
                    $paged = get_query_var('page');
                } else {
                    $paged = 1;
                }
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $loop = new WP_Query(
                    array(
                        'post_type' => 'post',
                        'posts_per_page' => get_option('posts_per_page'),     
                        'paged' => $paged,
                        'post_status' => 'publish',
                        'orderby' => 'desc',
                        'orderby' => 'date' // modified | title | name | ID | rand
                    )
                );
              ?>

              <?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>

                // Loop code goes here.

              <?php endwhile; ?>

              <?php if ($loop->max_num_pages > 1) : // custom pagination  ?>
                <?php
                  $orig_query = $wp_query; // fix for pagination to work
                  $wp_query = $loop;
                  $big = 999999999;
                  echo paginate_links(array(
                      'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                      'format' => '?paged=%#%',
                      'current' => max(1, get_query_var('paged')),
                      'total' => $wp_query->max_num_pages
                  ));                  
                  $wp_query = $orig_query; // fix for pagination to work
                ?>
              <?php endif; ?>

              <?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>  

            </div>

    <?php endwhile; ?>
    <?php endif; ?>    

<?php get_template_part('footer'); ?>

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