在 WordPress 中,我有一个页面模板,它使用网站其他部分没有使用的特定 JS 文件和 CSS 文件。在这个特定的模板页面上,有什么方法可以在调用 wp_head 之前将这些项目添加到头部?
我意识到我可以在条件中使用 is_page_template() 但我的头文件失去了控制。
如果你看这里http://codex.wordpress.org/Plugin_API/Action_Reference你可以看到在wp_head之前调用的钩子是get_header
所以你需要做的是:添加一个在该钩子上调用的操作,测试你是否在你想要的页面上,以及是否添加脚本和 css
这会发生在插件中(而不是在 header.php 或functions.php 等主题文件中),它看起来像这样:
// call the function that adds your current script
add_action('get_header', 'add_that_script');
function add_that_script()
{
if (is_page(SOME_PAGE_IDENTIFIER)) // this is the page you need; check http://codex.wordpress.org/Function_Reference/is_page on how to use this function; you can provide as a parameter the id, title, name or an array..
{
wp_register_script( 'mycustomscript', 'http://www.example.com/script.css'); // register your script
wp_enqueue_script( 'mycustomscript' ); // enqueue it to be added in the head section
wp_register_style( 'mycustomstyle', 'http://www.example.com/example.css'); // register your css
wp_enqueue_style( 'mycustomstyle' ); // enqueue it to be added in the head section
}
}
Run Code Online (Sandbox Code Playgroud)
您只需替换页面的 id 以及 js 文件和 css 的 url。当然,如果您在正确的页面上,也许您想测试其他方式,但我认为这表明了这个想法。
| 归档时间: |
|
| 查看次数: |
7965 次 |
| 最近记录: |