_page_template()是否在functions.php中不起作用
所以我有一个脚本,我只想包含在一个模板文件中。模板文件位于名为“我的孩子”主题中的“模板”文件夹中。我想我可以通过在functions.php中包含一个if语句并按名称将模板文件作为目标来实现,如下所示:_page_template()是否在functions.php中不起作用,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,所以我有一个脚本,我只想包含在一个模板文件中。模板文件位于名为“我的孩子”主题中的“模板”文件夹中。我想我可以通过在functions.php中包含一个if语句并按名称将模板文件作为目标来实现,如下所示: function header_anim() { wp_enqueue_script( 'head', get_stylesheet_directory_uri() . '/js/head.js' ); } if( is_page_template( '/templates/home-tem
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_脚本的函数中构造if语句,并使用条件if not than 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' );
希望我没有添加您已经尝试过的另一个迭代。让我知道它是如何工作的…感谢您的尝试,不幸的是,它没有工作,这很奇怪,因为它在控制台中没有吐出任何错误。我在functions.php中尝试了很多不同的代码变体,但都没有成功。还有其他想法吗?