此 if 语句将允许您使用以下方法之一检测 iOS 和移动设备访问者:
if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
$device = "ipad";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
$device = "iphone";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
$device = "blackberry";
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
$device = "android";
}
if( $device ) {
return $device;
} return false; {
return false;
}
}
即使您可以使用 .htaccess 文件,例如:
RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://www.yoururl.com [R=301]
在你的functions.php文件中:
add_action( 'template_redirect', 'device_redirect' );
function device_redirect(){
if ( is_front_page() && is_home() ) {
if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
wp_redirect( "http://www.example.com/iphone", 301 );
} else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
wp_redirect( "http://www.example.com/andriod", 301 );
}
}
}
如果您使用静态主页或博客页面,则必须更改 if。
例如
if ( is_front_page() && is_home() ) {
// Default homepage
}
if ( is_front_page()){
//Static homepage
}
if ( is_home()){
//Blog page
}