[Support request] Change number of post displayed on the first page of archives

Home Forums Support [Support request] Change number of post displayed on the first page of archives

Home Forums Support Change number of post displayed on the first page of archives

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2372064
    Fabien

    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 ?

    #2372073
    David
    Staff
    Customer Support

    Hi there,

    See Fernandos reply here:

    https://generatepress.com/forums/topic/different-number-of-posts-on-first-page/#post-2171894

    That PHP Snippet will offset the number of posts on Paged archives.

    In the code you will see 2 x instances of: $offset = -3; those will need changing to -1

    #2372174
    Fabien

    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 ?

    #2372765
    Fabien

    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.

    #2372871
    Fernando
    Customer Support

    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?

    #2373052
    Fabien

    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

    #2373070
    Fernando
    Customer Support

    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 );
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.