PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] Custom wp_query with working pagination on a page template

 March 13, 2022     loops, pagination, php, wordpress     No comments   

Issue

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.


Solution

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'); ?>


Answered By - Sam Assoum
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing