Change the default number of posts to show on mobile version

Firstly, you need to detect mobile devices, probably the easiest and a built in possibility is wp_is_mobile. It is simpler then other solutions that are available, but works reasonably well. It can be altered, if necessary, take a look at this question for a first insight into that.

Secondly, if that concerns your main query, like your code suggests, you can use pre_get_posts to alter it.

Below a basic example on how to bring those together:

Code:

add_action('pre_get_posts','wpse124949_alter_main_query_ppp_mobile');
function wpse124949_alter_main_query_ppp_mobile( $query ){
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    if( $query->is_main_query() && wp_is_mobile() ) {
        $query->set('posts_per_page', '15');
    } 
}

Leave a Comment