Site logo

Archived topic

I would like to get 12 post on the homepage and 16 on the other pages

9 replies · Started by jmarc on December 22, 2022

Viewing posts 1–10 of 10

Hello
everything is in the title. I can't find a way to do that. Can you please help ?
Thank you
MArc

Hi there,

Can you link us to the homepage and the other pages you are referring to?

Hello Ying

Yes ! In private message you can get the URL
Thank you

MArc

1. Go to settings > reading, and set the Blog pages show at most to 16.

2. Add this PHP snippet:

add_action( 'pre_get_posts', 'yh_query_offset', 1 );
function yh_query_offset( &$query ) {
        if ( is_admin() ) {
            return;
        }

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

        // Don't do anything to Elements.
        if ( 'gp_elements' === $query->get( 'post_type' ) ) {
            return;
        }

	// First, define your desired offset...
	$offset = -4;

	// Next, determine how many posts per page you want (we'll use WordPress's settings)
	$ppp = get_option( 'posts_per_page' );

	// Next, detect and handle pagination...
	if ( $query->is_paged ) {

		// Manually determine page query offset (offset + current page (minus one) x posts per page)
		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );

		// Apply adjust page offset
		$query->set( 'offset', $page_offset );

	}
	else {

		// This is the first page. Set a different number for posts per page
		$query->set( 'posts_per_page', $offset + $ppp );

	}
}

add_filter( 'found_posts', 'yh_adjust_offset_pagination', 1, 2 );
function yh_adjust_offset_pagination( $found_posts, $query ) {
        if ( is_admin() ) {
            return;
        }

	// Define our offset again...
	$offset = -4;
	//get posts per page from WP settings
	$ppp = get_option( 'posts_per_page' );
	$real_page_numbers =ceil(($found_posts - $ppp - $offset) / $ppp) + 1;

	// Ensure we're modifying the right query object...
	if ( is_main_query() && ! is_paged() ) {
		// Reduce WordPress's found_posts count by the offset...
		return $real_page_numbers * ($ppp + $offset);
	}
	 return $real_page_numbers * $ppp;
	
}

Adding PHP: https://docs.generatepress.com/article/adding-php/

Thank you but It also shows 16 in the right sidebar "Last post" ... and random image :(

The query in the sidebar is the same as the main query of the blog, these are repeated content which I don't think is good for SEO.

Will you consider removing them from the blog?

Noop, the plugin is "Recent Posts Widget With Thumbnails" and I want to keep it, I think It looks good on the website
If my demande is not possible : never mind ... or maybe you have another solution to fix this ?

Thank you for your kind help anyway
MArc

In that case, I would recommend reaching out to the plugin support and asking how to target their loop.

Show them the PHP snippet, they should be able to help!

I'll do that thank's a lot
Marc

No Problem, glad to help :)

This archived topic is closed to new replies.