如何仅为 WordPress 主题中的一个特定页面加载 jQuery? [英] How do I load jQuery for only one specific page in my WordPress theme?
问题描述
我正在摆弄一个 WordPress 主题.我知道我可以在 header.php 中使用 wp_enqueue_script
来加载 WordPress 提供的 jQuery 库.
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.
我只是打算在我的标题中使用 wp_enqueue_script,但是当我只想在特定页面上使用它时(仅在具有特定 page_id 的单个页面上)似乎效率低下.
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.)
鉴于此,仅在一个特定页面上包含 jQuery 的最佳方法是什么?
Given that, what's the best way of including jQuery only on one particular Page?
大概我不能在 header.php 中做 page_id 检测,因为那不会在循环中,对吧?但我猜我错过了一些相当明显的方法——我对主题开发还很陌生......
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...
谢谢!
推荐答案
可以,is_page
不需要在循环中调用,因为循环运行时它不会改变.所以 is_page(42)
只会返回 TRUE
如果您在 id 为 42 的页面上.它也适用于页面标题或名称(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. So is_page(42)
will only return TRUE
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.
这篇关于如何仅为 WordPress 主题中的一个特定页面加载 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!