Site logo

Archived topic

Custom number of posts on "Load More"

3 replies · Started by George on May 8, 2018

Viewing posts 1–4 of 4

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.

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!

You're welcome :)

This archived topic is closed to new replies.