is_page_template()在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语句。


#1


怎么样了?我对样式表所做的一件事可能与此脚本一起使用。正在构造调用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' );

让我知道这是否适合你…。

后来果汁


#2


嗯, 这怎么样。让我们尝试做一些不同的调节。…你是否还有另一个功能正在为其余模板/页面加载另一个脚本?如果是这样, 我们可以将它们组合为一个功能, 希望它能起作用…

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' );

希望我不只是添加你已经尝试过的另一个迭代。让我知道这是如何工作的…

来源:

https://www.srcmini02.com/63619.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37600.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?