is_page_template()在functions.php中不起作用
问题描述:
所以我有一个脚本,我只想包含在一个模板文件中。模板文件位于我的子主题中名为模板的文件夹中。我以为我可以通过包括如果functions.php的语句,并名字,像这样针对模板文件做到这一点:is_page_template()在functions.php中不起作用
function header_anim() {
wp_enqueue_script('head', get_stylesheet_directory_uri() . '/js/head.js');
}
if(is_page_template('/templates/home-template.php')) {
add_action('wp_enqueue_scripts', 'header_anim');
}
但是,好像它不承认if语句出于某种原因。
答
最近怎么样?有一件事我可以用我的样式表完成,可能适用于这个脚本。在调用wp_enqueue_script的函数中构造if语句,如果不是返回,则使用该条件。例如:
function header_anim() {
if(!is_page_template('/templates/home-template.php')){
return;
}
wp_enqueue_script('head', get_stylesheet_directory_uri() . '/js/head.js');
}
add_action('wp_enqueue_scripts', 'header_anim');
让我知道是否适合你....
后来, 果汁
答
嗯,这个怎么样。让我们尝试以不同的方式进行调节......您是否有另一个函数为其他模板/页面加载另一个脚本?如果这样我们就可以将它们组合成一个函数,并希望这将工作...
function equeue_scripts_header() {
if(is_page_template('/templates/home-template.php')){
wp_enqueue_script('head', get_stylesheet_directory_uri() . '/js/head.js');
}
else {
/*call enqueue script for that corresponds to the rest of the site */
}
}
add_action('wp_enqueue_scripts', 'equeue_scripts_header');
希望我不只是增加你已经尝试过另一次迭代。让我知道这个是如何工作的?
果汁
感谢您尝试,可惜这没有工作,因为它没有在控制台中吐出任何错误,它的怪异。我已经在functions.php中尝试了很多不同的代码,但它不会发生。任何其他想法? –