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;
}