open
27.08.2022.

Different posts per page on desktop and mobile devices – code snippet or wordpress plugin

Last update: 16.09.2022.

I found a great code snippet on the Stack Overflow that if you paste it into functions.php it will display a different page number on desktop or notebook and other mobile devices.

If you don’t want to change the code or switch wordpress themes often, I created a wordpress plugin where you can customize the values. This feature overrides the default reading settings.

The demo works on the site look at it on desktop and mobile and you can see.


add_action( 'pre_get_posts', 'change_posts_per_page' );
function change_posts_per_page( $query ) {
if( !$query->is_main_query() || is_admin() ) return;
// 3 for mobile devices 5 for desktop or notebook
$posts_per_page = wp_is_mobile() ? 3 : 5;
$query->set( 'posts_per_page', $posts_per_page );
return;
}
Copy to Clipboard


Related posts

Got a question? Is something not working? Write and I'll answer!

Leave a Reply

Your email address will not be published.

11 − 1 =