以下内容将帮助您识别操作系统和浏览器,并将类添加到body标记中,以便在加载页面时参考。您可以修改整个过程,以便它确定要使用哪个下载链接。这就是我用来区分CSS的目的,但最终你想要的是识别操作系统。
strtolower( $_SERVER[\'HTTP_USER_AGENT\'] );
标识正在使用的浏览器。getenv( \'HTTP_USER_AGENT\' );
获取环境/操作系统。
<?php
function os_browser_body_class( $class ) {
$brwsr = array(
\'msie\',
\'firefox\',
\'webkit\',
\'opera\'
);
$os = array(
\'Windows\',
\'Mac\', //will include Mac OS & Apple iOS
\'Macintosh\' //just Mac OS
);
$user_brwsr = strtolower( $_SERVER[\'HTTP_USER_AGENT\'] );
$user_agent = getenv( \'HTTP_USER_AGENT\' );
foreach( $brwsr as $name ) {
if( strpos( $user_brwsr, $name ) > -1 ) {
$class[] = $name;
preg_match( \'/\' . $name . \'[\\/|\\s](\\d)/i\', $user_brwsr, $matches );
if ( $matches[1] )
$class[] = $name . \'-\' . $matches[1];
return $class;
}
}
foreach( $os as $name ) {
if( strpos( $user_agent, $name ) > -1 ) {
$class[] = $name;
preg_match( \'/\' . $name . \'[\\/|\\s](\\d)/i\', $user_agent, $matches );
if ( $matches[1] )
$class[] = $name . \'-\' . $matches[1];
return $class;
}
}
return $class;
}
add_filter( \'body_class\', \'os_browser_body_class\' );
?>
您也可以通过以下方式简化它:
<?php
$user_agent = getenv( \'HTTP_USER_AGENT\' );
if( $user_agent == \'Windows\' ) :
$app_download = \'http://downloadurl.com/to-your-app/windows-version.zip\';
elseif( $user_agent == \'Mac\' ) :
$app_download = \'http://downloadurl.com/to-your-app/mac-version.zip\';
endif;
?>
然后在您的页面上,您只需使用回音将适当的链接放置到位:
echo $app_download;