简体   繁体   English

Wordpress&Enquire.js-条件加载原理

[英]Wordpress & Enquire.js - conditional loading philosophy

Is not a matter of code, I just don't get the idea of how things works in this specific case. 与代码无关,我只是不知道在这种特定情况下事情如何工作。

I'm woking on a Wordpress theme which should dynamically load portions of php code through javascript. 我正在使用Wordpress主题,该主题应通过javascript动态加载部分php代码。 It actually works combined with UA Sniffing because if the device is mobile, the mobile template should be used anyway but that's not the point. 它实际上可以与UA嗅探结合使用,因为如果设备是移动设备,则无论如何都应使用移动模板,但这不是重点。

  • If the screen.width is < 1080px the Mobile version menu of my Wordpress theme should be loaded. 如果screen.width <1080px,则应加载我的Wordpress主题的移动版本菜单。
  • otherwise, if the screen.width is bigger, the desktop menu version should be loaded 否则,如果screen.width较大,则应加载桌面菜单版本
  • a solution based just on CSS Media queries is not acceptable since the desktop version is too heavy. 由于桌面版本太重,因此仅基于CSS Media查询的解决方案是不可接受的。

So I decided to conditional dynamically load content through enquire.js The first part of my WP-homepage-template.php is: 因此,我决定有条件地通过enquire.js动态加载内容。WP -homepage-template.php的第一部分是:

enquire.register("screen and (max-width:1080px)", {
<script type="text/javascript">
    match : function() {
        $("body").load(
          "/path/headernav-phone.php"
        );

    unmatch : function() {
            $("body").load(
          "/path/headernav-desktop.php"
        );
    }
}

So, when the size of the screen hits 1080px, Wordpress should to load headernav-phone.php which contains just a wp_nav_menu(MOBILE) call. 因此,当屏幕大小达到1080px时,Wordpress应该加载headernav-phone.php,其中仅包含wp_nav_menu(MOBILE)调用。 If the screen size is bigger, Wordpress loads headernav-desktop.php which includes wp_nav_menu(DESKTOP). 如果屏幕较大,则Wordpress会加载headernav-desktop.php,其中包括wp_nav_menu(DESKTOP)。

This doesn't work: 这不起作用:

Fatal error: Call to undefined function wp_nav_menu() in /XX/YYY/ZZZ/webseiten/XXX.YY/wp-content/themes/MyTheme/templates/headernav-phone.php on line 14 致命错误:在第14行的/XX/YYY/ZZZ/webseiten/XXX.YY/wp-content/themes/MyTheme/templates/headernav-phone.php中调用未定义函数wp_nav_menu()

It looks like that my code is run before the CMS, and it breaks its own functions. 看来我的代码在CMS之前运行,并且破坏了自己的功能。

IMO, the logic is flawed. 海事组织,逻辑有缺陷。
For one, there's no way to detect the screen width with PHP . 首先, 无法使用PHP检测屏幕宽度

Secondly, if you were to generate a PHP -> JS file ( my-script.js.php ), you'd have to load WordPress engine two times . 其次,如果要生成my-script.js.php > JS文件( my-script.js.php ), 则必须加载WordPress引擎两次
The Answer in this link is super-pro . 此链接中的答案是super-pro You'll find easier methods that use wp-load.php and WP_USE_THEMES . 您会发现使用wp-load.phpWP_USE_THEMES简便方法。

Thirdly, the way you're trying to load a theme template would require the "load two times" approach. 第三,您尝试加载主题模板的方式将需要“两次加载”方法。 Or solving it with AJAX and wp_localize_script . 或者用AJAXwp_localize_script 解决

If I understood the Question correctly, I think the best bet would be to use WordPress user-agent detection and enqueue your scripts and styles accordingly. 如果我正确理解了问题,我认为最好的选择是使用WordPress用户代理检测并相应地排入脚本和样式 Study the core function to adapt to your needs. 研究核心功能以适应您的需求。

[update] [更新]

Still not sure if all this complexity is really needed. 仍然不确定是否真的需要所有这些复杂性。 Would something like this work in a generic header.php ? 这样的事情在通用的header.php吗?

if( wp_is_mobile() )
    wp_nav_menu(MOBILE);
else
    wp_nav_menu(DESKTOP);

But, if you really need this changes to happen dynamically, then AJAX would be the choice. 但是,如果您确实需要动态地进行此更改,则可以选择AJAX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM