2017-10-08 26 views
0

所以我有一個腳本,我只想包含在一個模板文件中。模板文件位於我的子主題中名爲模板的文件夾中。我以爲我可以通過包括如果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語句出於某種原因。

回答

1

最近怎麼樣?有一件事我可以用我的樣式表完成,可能適用於這個腳本。在調用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'); 

讓我知道是否適合你....

後來, 果汁

+0

感謝您嘗試,可惜這沒有工作,因爲它沒有在控制檯中吐出任何錯誤,它的怪異。我已經在functions.php中嘗試了很多不同的代碼,但它不會發生。任何其他想法? –

0

嗯,這個怎麼樣。讓我們嘗試以不同的方式進行調節......您是否有另一個函數爲其他模板/頁面加載另一個腳本?如果這樣我們就可以將它們組合成一個函數,並希望這將工作...

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

希望我不只是增加你已經嘗試過另一次迭代。讓我知道這個是如何工作的?

果汁

相關問題