[英]is_page_template() not working in functions.php
所以我有一个脚本,我只想包含在模板文件中。 模板文件位于我的子主题中名为模板的文件夹中。 我想我可以通过在 functions.php 中包含一个 if 语句并按名称定位模板文件来做到这一点,如下所示:
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 语句,并使用 if not 条件而不是 return。 例如:
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' );
希望我不只是添加您已经尝试过的另一个迭代。 让我知道这个是如何工作的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.