Custom wp_query with working pagination on a page template












0














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.










share|improve this question





























    0














    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.










    share|improve this question



























      0












      0








      0







      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.










      share|improve this question















      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.







      php wordpress loops pagination






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 20:29

























      asked Nov 22 at 20:16









      Sam Assoum

      9919




      9919
























          1 Answer
          1






          active

          oldest

          votes


















          0














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





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437554%2fcustom-wp-query-with-working-pagination-on-a-page-template%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














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





            share|improve this answer


























              0














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





              share|improve this answer
























                0












                0








                0






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





                share|improve this answer












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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 6 at 15:52









                Sam Assoum

                9919




                9919






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437554%2fcustom-wp-query-with-working-pagination-on-a-page-template%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)