如何仅为 WordPress 主题中的一个特定页面加载 jQuery?
我正在摆弄 WordPress 主题。我知道我可以在 header.php 中使用 wp_enqueue_script
来加载 WordPress 提供的 jQuery 库。
我只想在标头中使用 wp_enqueue_script ,但当我只想在特定页面上使用它时(仅在具有特定 page_id 的单个页面上),它似乎效率低下。
鉴于此,包含 jQuery 仅在一个特定页面上?
想必我无法在 header.php 中进行 page_id 检测,因为它不会出现在循环中,对吧?但我猜我错过了一些相当明显的方法——我对主题开发相当陌生......
谢谢!
I'm fiddling with a WordPress theme. I'm aware I can use wp_enqueue_script
in my header.php to load WordPress's supplied jQuery library.
I was just going to use wp_enqueue_script in my header, but it seems inefficient when I only want to use it on a particular Page (just on one single page with a particular page_id.)
Given that, what's the best way of including jQuery only on one particular Page?
Presumably I can't do page_id detection in header.php, because that won't be in The Loop, right? But I'm guessing I'm missing some fairly obvious method -- I'm fairly new to theme development...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以,
is_page
不需要在 The 中调用循环,因为循环运行时它不会改变。因此,如果您所在的页面 ID 为 42,则is_page(42)
只会返回TRUE
。它也适用于页面标题或名称 (slug),这可能是如果您曾经替换删除此页面并将其替换为具有相同 slug 的新页面,则更加面向未来。Yes you can,
is_page
doesn't need to be called in The Loop, since it doesn't change when The Loop runs. Sois_page(42)
will only returnTRUE
if you're on the page with id 42. It also works with the page title or name (slug), which might be more future-proof if you ever replace delete this page and replace it with a new one with the same slug.这是一篇关于动态主体 ID 的文章
http://perishablepress.com/press /2009/05/26/dynamic-body-class-id-php-wordpress/
获得页面名称后,您可以在模板 index.php 中添加一条条件语句,在页面标题中或结束正文标记之前显示类似以下内容:
here is an article about dynamic body ids
http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/
after you get your page name you can add a conditional statement in your template index.php that says something like this in your page header or before the closing body tag: