1

I've been building a theme from scratch so writing everything from nothing and every place I go online which teaches me how to implement pagination are mostly showing ways to make the standard Wordpress version into a style one.

I've also learnt there's about 3 different paginations (Prev/Next, Archive Pages, Page Break) and I assume these all require different codes.

At the moment I've successfully implemented the Prev/Next pages here: Image

This was achieved with:

<div class="postnav six columns droided-box prev clearfix">
    <h2>Previous Post</h2>
  <?php previous_post_link('%link', '%title', FALSE); ?>
    </div>
<div class="postnav six columns droided-box next clearfix">
    <h2>Next Post</h2>
    <?php next_post_link('%link', '%title', FALSE); ?>
</div>

I'm currently trying to add the pagination to the Archive.php page. Here's what I have for the archive page (ignore the title part, I've commented them out as I'm still deciding on the best way to enter it.

archive page

        <?php get_header(); ?>

    <div class="container">

      <?php get_template_part('dynbar','main'); ?>

      <div class="two-thirds column row droided-box droided-body droided-page">
        <?php if ( have_posts() ) : ?>
        <h1>Posts from
          <?php // single_cat_title(); ?>
        </h1>
        <?php // if ( category_description() ) : ?>
        <?php // echo category_description(); ?>
        <?php // endif; ?>
      </div>
      <?php while ( have_posts() ) : the_post(); ?>
      <div class="two-thirds column row droided-box droided-body droided-item">
        <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
          <?php the_title(); ?>
          </a></h1>
        <?php the_post_thumbnail('featured'); ?>
        <?php echo excerpt(200); ?>
        <p class="top-meta">Written by
          <?php the_author_posts_link() ?>
          on
          <?php the_date(); ?>
        </p>
        <div class="post-meta">
          <p>
            <?php
      comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments closed'); ?>
          </p>
        </div>
        <a class="read-more-droided" href="<?php the_permalink(); ?>">Continue Reading...</a> </div>
      <?php endwhile; else: ?>
      <p>Sorry, no posts matched your criteria.</p>
    </div>
    <?php endif; ?>

    <?php the_posts_pagination( array(
        'mid_size' => 2,
        'prev_text' => __( 'Back', 'droided' ),
        'next_text' => __( 'Onward', 'droided' ),
    ) ); ?>

    <?php get_footer(); ?>

That's part of an archive page for June which lists a couple posts. Now I haven't entered any code in the functions.php in regards to pagination because I'm not sure if I need to because it's already wordpress core function right?

Is there a default cut off for archive pages which breaks the page because I haven't entered it anywhere.

My main question is how can I check to see if pagination is working and do I need to put anything in functions.php for the archive pagination to work, also is there a way to change the amount of posts the archive page to list before breaking to another page?

closed as unclear what you're asking by Michael, Robert hue, Howdy_McGee, cybmeta, Nicolai Jul 14 '15 at 10:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

The homepage and all archive pages (date, category, tag, taxonomy and author pages ) uses the same default pagination functions for pagination. What this means is, the_posts_pagination works on all archive pages and the home page. The only differences on these pages is the content which is determined by the main query.

As far as pagination goes, it is calculated the same on all of these pages, no difference here, and this also goes for the main query and all custom instances of WP_Query. WP_Query use the following parameters to calculate pagination

  • posts_per_page - This value is constant across the complete site, including custom queries. WP_Query uses the amount of posts per page set in the backend under the reading settings page to calculate how many posts to show per page when no custom value is set (like for custom queries). This default option can be returned with get_option( 'posts_per_page' ). If you need a custom value for the main query for a specific archive page or the home page, you should use pre_get_posts to set this custom value

  • offset - To page a query, WP_Query uses offset to skip a set of posts in order to show the correct posts according to page number you are on. The current page number can be returned with get_query_var( 'paged' ). Page 1 will be 0 and there after the value of get_query_var( 'paged )will be equal to the page number. Just as example, ifposts_per_pageis set to10, and you are viewing page 3,WP_Queryhas used an offset of20` to skip the first 20 posts to correctly show posts 21 - 30 on page 3

  • $found_posts - This property holds the amound of posts found by WP_Query which matched the query. By default, WP_Query does not stop searching the db once it found the amount of posts queried. It will continue throughout the db and count all the posts that matches the current query. So, if you have 10 posts per page, WP_Query will return only 10 posts in the query, but it does not bail out once that 10 posts is filled. It continue to count posts that matches the query, so if there are 100 posts matching the current query, the value of $found_posts will be 100. You can check this by echoing the result of $wp_query->found_posts

All of the above goes into the calculation of the $max_num_pages property of the WP_Query object. This is the important value used by pagination functions like the_posts_pagination to display the correct amount of pages and when to display pagination links. If the value is 1, the paging links will not show, these links only show if the value is bigger than 1. By default, the main query object value is passed to these pagination functions. You can check the value of this property for the main query object by echoing the value of $wp_query->max_num_pages.

You can do the following on your archive page (or any page) to display how many pages there are, what the setting of posts per page is and on which page we are

$ppp = get_option( 'posts_per_page' );
$page_number = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1
$total_pages = $wp_query->max_num_pages;
$show_pagination = get_query_var( 'paged' ) ? 'show' : 'not show';

echo 'My posts_per_page setting for this page is ' . $ppp . ' with an amount of ' . $total_pages . ' pages of posts. According to this, my pagination function should ' . $show_pagination . '. Do not forget, I am currently viewing page ' . $page_number . '.';
  • Thank you :) I struggle to just 'trust' things will work as intended unless I can see them and add my styling. I downloaded a demo XML which imported a bunch of posts which I was able to check the pagination and it does just work. – LukeScotney93 Jun 28 '15 at 15:45
  • My pleasure. Just one very very important note here about trusting. The main query object is saved in the global variable $wp_query. This global variable is broken by query_posts. This is why we hammer so hard that you must never ever use query_posts :-) Enjoy – Pieter Goosen Jun 28 '15 at 16:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.