我在wordpress主题上有一个移动绝对页面。
关于检测手机和平板电脑,我已经完成了。
当我使用手机或平板电脑时,如何重定向到子文件夹或移动文件。
谢谢大家。
答案 0 :(得分:0)
有两种方法可以重定向到移动子目录:
1)最简单,最简单:
使用以下javascript,将其放在html页面的开头
<script type="text/javascript">
<!--
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
//-->
</script>
2)有点复杂的编辑.htaccess文件:
# Check for mime types commonly accepted by mobile devices
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]
将您的网址重写为移动子目录。
答案 1 :(得分:0)
我有一个解决方案。
我在wordpress主题的函数中编写代码。
if(!is_admin()){
//This file help us detect mobile base on user agent.
require_once ('mobile_detect.php');
$mobile_detect = new Mobile_Detect();
function load_mobile() {
if(is_home())
$template_name = get_home_template();
elseif(is_page())
$template_name = get_page_template();
elseif(is_single()) {
$post_type = get_post_type() !== 'post'?'-'.get_post_type():'';
$template_name = 'single'.$post_type.'.php';
} elseif (is_archive()) {
$archive_type = get_query_var('term');
$template_name = get_template_directory().'/archive'.$archive_type.'.php';
}
$template_name = str_replace('.php', '', $template_name);
return $template_name.'-sp.php';
}
if($mobile_detect->isMobile())
add_filter('template_include', 'load_mobile');
}
当我访问网站时,该编码将加载移动版本文件(* -sp.php)。
编码不好。我试图获取文件名称。但我现在不能。