Set number of article per number of page

I’m making a timeline using this awesome free theme.

As you can see in this theme, it’s a timeline separated by “Page”.
We have “Page 1” with 5 articles, “Page 2” with 5 articles, etc …

I changed this by :

  • “Page 1” become -> September
  • “Page 2” become -> October
  • “Page 3” become -> November

to adapt this theme in real timeline. It works great.

My ultimate problem is that I don’t know how to set number of articles per page number. It’s like 4 articles per all pages, that’s it. ( Ofcourse, I know how to set this value ).

Like this : I wrote 3 articles in September, and 4 in October.
With this usecase, my 3 articles wrotes in September will be in “September page” , and the first of article wrote in October will be also in “September page”.

So my question is simple, can i set the number of articles per number page ?

  • Page 1 ( Septembre ) : 4 articles max.
  • Page 2 ( October ) : 8 articles max.
  • Page 3 ( November ) : 1 articles max.

After some searchs, i saw this code sample :

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  global $wp_the_query;

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 3 );
  }
  elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 5 );
  }
  // Etc..

  return $query;
}

source : Change Posts per page count

This is exactly what i want, but no on “article category” but on page number

as (my pseudo code)

if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_page(1) ) {
        $query->set( 'posts_per_page', 4 );  // **SEPTEMBER**
      }
if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_page(2) ) {
            $query->set( 'posts_per_page', 3 ); // **OCTOBER**
          }

1 Answer
1

Pagination is result of combining two factors: page size and offset. In common case page size is constant and offset is page size times current page.

Is it possible to build highly elaborate pagination you describe? Technically yes. That would be matter to adjusting page size and offset in a more elaborate fashion.

However specific of such implementation would be highly inconvenient. Imagine you are on a page 15. How do you determine which month it is? You would need to backtrack all the way to the start and rebuild page by page 14 previous pages to get a clue at which point are you. Note that very likely not all months will have posts, even if that’s the case right now.

What could you do instead? Use date archives. WP is perfectly capable of producing pages which hold time–based archive for the specific month (e.g. example.com/2016/06/) out of the box.

On any specific month you know exactly at which point in time you are, the rest is just matter of providing UI/links to other months, instead of typical pagination (for example with wp_get_archives()).

Leave a Comment