0

I found this on Stackoverflow: https://stackoverflow.com/questions/21135388/wordpress-change-the-limit-of-the-number-of-posts-just-for-mobile

I implemented the answer, yet it didn't work. I'm working with the AccessPress Mag template, post count on larger screens is set to unlimited.

This is the code I tried:

add_action('pre_get_posts','change_limit_mobile');

function change_limit_mobile($query){

    $new_limit = 10;

    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

    if (( $iphone || $android || $ipad || $ipod || $berry ) && $query->is_main_query()){
        set_query_var('posts_per_page',$new_limit);
    }
}

I tried adding it to functions.php and the index.php pages, but no luck.

Has anything changed since the latest update, or do I miss something here?

1

Original answer: You could instead try using MobileESP by downloading https://raw.githubusercontent.com/ahand/mobileesp/master/PHP/mdetect.php to your theme directory and then:

function change_limit_mobile( $query ) {
    if ( $query->is_main_query() ) {
        require get_stylesheet_directory() . '/mdetect.php';
        $mdetect = new uagent_info;
        if ( $mdetect->DetectMobileLong() ) {
            set_query_var( 'posts_per_page', 10 );
        }
    }
}

Update: the simplest thing is to use wp_is_mobile() - probably good enough!

Theme-specific update: In the case of the theme AccessPress Mag you're using, then it uses its own options to control the number of posts in the special home-page block loops. One way to override these is to define your own version of the pluggable theme framework options function in "functions.php":

function of_get_option( $name, $default = false ) {

    $option_name = '';

    // Gets option name as defined in the theme
    if ( function_exists( 'optionsframework_option_name' ) ) {
        $option_name = optionsframework_option_name();
    }

    // Fallback option name
    if ( '' == $option_name ) {
        $option_name = get_option( 'stylesheet' );
        $option_name = preg_replace( "/\W/", "_", strtolower( $option_name ) );
    }

    // Get option settings from database
    $options = get_option( $option_name );

    // Return specific option
    if ( isset( $options[$name] ) ) {
        /******* Addition Begin *******/
        // NOTE: Can add other options here...
        if ( 'posts_for_block1' === $name ) {
            return change_limit_mobile( $options[$name] );
        }
        /******* Addition End *******/
        return $options[$name];
    }

    return $default;
}

where change_limit_mobile() is now:

function change_limit_mobile( $value ) {
    if ( wp_is_mobile() ) {
        $value = 10;
    }
    return $value;
}
| improve this answer | |
  • I tried adding mdetect.php to my theme's directory and then adding your code to functions.php. That resulted in an error, website didn't load anymore. – Michael Nov 7 '15 at 17:45
  • Ok maybe your include path isn't set, so an explicit path with get_stylesheet_directory() might be better - will update answer... – bonger Nov 7 '15 at 17:55
  • I'm afraid I keep getting a 500 error. I do use your code in the functions.php page right, position doesn't matter? – Michael Nov 7 '15 at 17:59
  • Yes "functions.php" is the right file, and position (now, with the absolute path) shouldn't matter. If you go into admin > Appearance > Editor, does "mdetect.php" appear in the list of templates on the right hand side, and if so if you click it does it load in the textbox? – bonger Nov 7 '15 at 18:15
  • Yes it does, please see this link (image): imgur.com/YIZ5yB4 – Michael Nov 7 '15 at 18:17

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.