首页 文章

如何为手机版设置不同的静态主页? (WordPress的)

提问于
浏览
14

我想为我的网站移动版提供不同的静态主页 . 它不是一个真正的额外移动版本,但它是响应 .

我现在将全屏图像滑块设置为静态主页 . 由于网站的重新构建,这可以扩展到屏幕大小,但在移动设备(如iPhone)上看起来不太好 . 所以我有这个其他主页模板,我想在移动设备上查看网站时使用 .

这可以通过任何插件完成,还是应该通过编码完成?我不想使用主题切换器或类似的东西,我只想为移动设备设置不同的静态页面集 .

谁知道怎么做?

7 回答

  • 0

    这对我很好:

    function so16165211_mobile_home_redirect(){
        if( wp_is_mobile() && is_front_page() ){
            include( get_template_directory() . '/home-mobile.php' );
            exit;
        }
    }
    
  • 9

    您可以使用wp_is_mobile来检查移动设备,并在检测到移动设备时挂钩到template_redirect以加载其他模板:

    function so16165211_mobile_home_redirect(){
        if( wp_is_mobile() && is_front_page() ){
            include( get_template_directory() . '/home-mobile.php' );
            exit;
        }
    }
    add_action( 'template_redirect', 'so16165211_mobile_home_redirect' );
    
  • 10

    我将Mobile-Detect包含在其自己的文件夹中的主题中,并在 header.php 的开头添加此代码:

    if( is_front_page() ){
    
        include_once('mobile-detect/Mobile_Detect.php');
        $detect = new Mobile_Detect(); 
    
        if ( $detect->isMobile() || $detect->isTablet() ) {
            $redirect_url = 'http://example.com/mobile_home';
            header('Location: ' . $redirect_url ); // Redirect the user
        }
    }
    

    您可以根据需要自定义此解决方案 . 我已经将它用于几个项目以获得类似的解决方案 .

  • 0

    这应该工作:(在functions.php中插入)

    //* Redirect homepage on mobile
    add_action( 'wp_head', 'wps_params', 10 );
    function wps_params() {
        ?>
        	<script>
    	if (window.location.pathname == '/' && jQuery(window).width() <= 480) {
    	   window.location = "/webshop/";
    	}
    	</script>
    
        <?php
    }
    

    将“/ webshop /”替换为移动主页的链接 .

  • 0

    您可以尝试使用detectmobilebrowsers脚本 .

    我记得它只是一个php文件,并且有一个功能,询问如何处理不同的移动设备(iPhone,iPad,机器人,Windows手机,BlackBerry和Palm设备) .

    通过转到此function generator页面,您可以更好地了解脚本的工作原理 .

  • 1

    它很简单,无需编码所有内容 . 从wordpress存储库安装“重定向”插件 . 1.转到设置页面 . 2.使用默认桌面主页输入“源URL”3.在“匹配”选项中,选择“URL和用户代理”,然后在“操作”选项中选择“重定向到URL” . 单击“添加重定向” . 4.将出现新的配置选项 . 给你想要的任何头衔 . “源URL”必须为空(表示这是您的基本主页) . 在“用户代理”选项中,选择是iPhone还是Android . 在“匹配”选项上,为移动主页设置所需的重定向 .

    完成!

    您肯定可以根据您之前使用该插件设置的重定向来区分桌面和移动设备上的主页 . 但是,您不能使用相同的网址名称(例如:桌面www.abcde.com和移动设备的www.abcde.com/mobilehomepage) .

  • 0

    将以下内容添加到您的functions.php应该可以做到这一点:

    //* Redirect homepage on mobile
    
    add_action( 'wp_head', 'wps_params', 10 );
    
    function wps_params() {
    ?>
    
    <script>
    if (window.location.pathname == '/' && jQuery(window).width() <= 480) {
       window.location = "/webshop/";
    }
    </script>
    
    <?php
    }
    

相关问题