Site logo

Archived topic

Change number of post displayed on the first page of archives

6 replies · Started by Fabien on October 13, 2022

Viewing posts 1–7 of 7

Hi,

I am using the "Make First Post Featured" (width: 100%) in order to feature the latest post on archive pages.

By doing this, the grid below is not complete (12 posts - 1 post featured = 11 posts / 3 columns = not an interger). Then the last space of the grid is empty.

Thus, I've changed the number of post on the first page of archives using the following function :

/**
 * Archive - Changing the number of posts on page 1.
 */

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! $query->is_main_query() ) {
        return;
    }

    if ( ( ( ! $query->is_paged ) && ( !is_admin() ) ) && ( ( is_category() ) || ( is_home() )  ) ) {
        $query->set( 'posts_per_page', 13 );
    }
}, 1 );

It works well. However, the last post displayed is also displayed on the top of page 2.

Any idea on how to fix this ?

Hi @david,

Thanks for the first reply.

The snippet provided works but :

  • Offset needs to be set to -2 in order to have a completed grid on the first page
  • The first page displays only 10 posts (1 featured + 9 posts). My goal is to have 13 posts (1 featured + 12 posts)

Any idead on how to achieve this ?

Aslo something I've noticed, when using Fernandos code with this conditional tags :

// Before anything else, make sure this is the right query...
  if (  ! $query->is_home()  ) {
  return;
}

The issue doesn't appear with other conditional tags such as :

// Before anything else, make sure this is the right query...
  if (  ! $query->is_category()  ) {
  return;
}

It breaks everything done with GP Elements Blocks.

Can you please try on your end ? Seems like a bug IMO.

Hi Fabien,

To clarify, how many posts do you want displayed in page 1, and on other pages?

Moreover, will this be applied to just the category archive pages or also in your blog or other archive pages?

Hi Fernando,

What I would like to achieve:

  • 13 posts on the first page, 12 posts on the other pages
  • Targeting the categories pages (is_category) and the home page of the blog (is_home)

Thanks,

Fabien

Go to Settings > Reading and set the Blog pages show at most to 13.

Then, remove the snippets you have for this, and add this instead:

function itsme_category_offset( $query ) {
    $ppp = get_option( 'posts_per_page' );
    $first_page_ppp = $query->query_vars[ 'paged' ];
	$current_page = get_query_var('paged');
    $paged = 12;

    if( !is_admin() && ( $query->is_home() || $query->is_category() ) && $query->is_main_query() ) {
        if( is_paged() ) {

            $query->set( 'posts_per_page', $paged );
			$paged_offset = $ppp + ( $paged *  ( $current_page - 2 ) );
			if($paged_offset < 0) {
				$paged_offset = $ppp;
			}
        	$query->set('offset', $paged_offset );

        }
    }
}
add_action( 'pre_get_posts', 'itsme_category_offset' );

function itsme_adjust_category_offset_pagination( $found_posts, $query ) {
    $ppp = get_option( 'posts_per_page' );
    $first_page_ppp = $query->query_vars[ 'paged' ];
	

    if( !is_admin() && ( $query->is_home() || $query->is_category() ) && $query->is_main_query() ) {
		if( is_paged() ) {
			return( $found_posts + 1 );
		} else {
			return( $found_posts - 1 );
		}
        
    }
    return $found_posts;
}
add_filter( 'found_posts', 'itsme_adjust_category_offset_pagination', 10, 2 );
This archived topic is closed to new replies.