is_front_page() 不工作 WordPress
当我位于首页时,我试图从“functions.php”加载脚本。
我在阅读选项中设置了一个名为“主页”的静态页面。
主页正确加载“front-page.php”模板,但条件脚本加载不起作用。
这就是我的“functions.php”文件中的内容:
wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);
if (is_front_page()) {
wp_enqueue_script('nivoslider');
}
为什么没有按预期加载?这里发生了什么事?
I'm trying to load a script from "functions.php" just when i'm in the front page.
I set a static page called "home" in the reading options.
The home page loads the "front-page.php" template correctly but the conditional script loading doesn't work.
This is what I have in my "functions.php" file:
wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);
if (is_front_page()) {
wp_enqueue_script('nivoslider');
}
Why isn't this loading as expected? What's happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 法典 -
仅将其设置为使用首页模板并不会使其成为首页。当满足上述任一条件时,它就是首页。因此,对于您创建的此页面,两者都必须为 false。
另外,这只是包含在functions.php中还是你实际上挂在了头部?
From the Codex -
Just setting it to use your front page template does not make it a front page. It is the front page when either of the conditions above are true. Therefore both must be false for this page you created.
Also, is this just included inline with functions.php or are you actually hooking into the head?
is_front_page();
不会按预期在functions.php中工作,因为functions.php在能够检测到您的“front_page.php”之前就已初始化is_front_page();
will not work in functions.php as expected since the functions.php initializes before its able to detect your "front_page.php"以下是对脚本进行排队的正确方法:
Here's the right way to enqueue scripts: