[Resolved] Custom number of posts on "Load More"

Home Forums Support [Resolved] Custom number of posts on "Load More"

Home Forums Support Custom number of posts on "Load More"

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #570893
    George

    Is there a way for the Load More button on a blog page to load a custom number of posts that is different from the one set in Settings->Reading->Blog pages show at most? For example I want to load 20 posts initially and then load more posts in increments of 10.

    #571085
    Tom
    Lead Developer
    Lead Developer

    Currently the load more button grabs the posts from the next page and loads them (yoururl.com/page/2 etc..).

    Perhaps something like this will help?: https://sridharkatakam.com/changing-posts-per-page-first-page-without-breaking-pagination-wordpress/

    #571277
    George

    Great, didn’t think I was going to pull this off but it actually worked thanks to you Tom!

    Here is the updated code for the “videos” category page:

    function gm_videos_query_offset( &$query ) {
    	if ( !( $query->is_category( 'videos' ) && $query->is_main_query() ) || is_admin() ) return;
        $offset = 10;
      	$ppp = get_option( 'posts_per_page' );
        if ( $query->is_paged ) {
    		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
            $query->set( 'offset', $page_offset );
        } else $query->set( 'posts_per_page', $offset + $ppp );
    } 
    
    add_action( 'pre_get_posts', 'gm_videos_query_offset', 1 );
    
    function gm_video_adjust_offset_pagination( $found_posts, $query ) {
    	$offset = 10;
      	if ( is_admin() ) return;
        else if ( $query->is_category( 'videos' ) && $query->is_main_query() ) return $found_posts - $offset;
        return $found_posts;
    } 
    
    add_filter( 'found_posts', 'gm_video_adjust_offset_pagination', 1, 2 );

    Thanks again!

    #571571
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.