3

I want to change the default number of posts shown for my mobile site. Here is my theme's loop

          <?php /* Start the Loop */ ?> 
          <?php while ( have_posts() ) : the_post(); ?>
              <?php get_template_part( 'mobile-content' ); ?>    
            <?php endwhile; ?>

I have 25 posts showing on desktop version set in wordpress settings but how can I show 15 posts per page for mobile version?

3

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');
    } 
}
|improve this answer|||||
0

You can use this function in your child theme functions.php or as a plugin to show 15 posts per page on mobile devices.

function custom_posts_per_page($query) {
    if (wp_is_mobile()) {
        $query->set('posts_per_page', 15);
    } //endif
} //function

//this adds the function above to the 'pre_get_posts' action     
add_action('pre_get_posts', 'custom_posts_per_page');
|improve this answer|||||
  • Please add an explanation to your answer: why could that solve the problem? – fuxia May 13 '15 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.