2014-10-31 35 views
0

我拼命讓我的WordPress的enque腳本工作。不知道我做錯了什麼,嘗試了很多,但似乎沒有任何工作。WordPress的if(if_page)和頁面模板不工作

我有一個tittle Ledenlijst,id = 69的頁面。 我只想對該頁面進行排隊。所以我試圖增加到functions.php:

// Funtion to include stylesheet for page ledenlijst. 
function enqueue_child_theme_styles() { 
    wp_enqueue_style('parent-style', get_template_directory_uri().'/style.css'); 
    wp_enqueue_style('child-style', get_stylesheet_uri(), array('parent-style') ); 
} 
// Function to add ledenlijst 
function add_ledenlijst() { 
    wp_enqueue_script(
     'your-script', 
     get_template_directory_uri() . '-child/ledenlijst.js', 
     array('jquery') 
    ); 
} 

if(is_page('ledenlijst')){ // Only on ladenlijst page 
echo "Page ledenlijst identified"; // debug 
    // Register Stylesheet 
    add_action('wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX); 
    // Register ledenlijst script 
    add_action('wp_enqueue_scripts', 'add_ledenlijst'); 
} 

由於某些原因,它不被識別。我也嘗試過: 如果(is__page(69), A已經複製了默認頁面,在我的子主題文件夾中創建了pageLedenlijst.php,將該主題應用到此頁面並嘗試: if(is_page_template('pageLedenlijst.php')

沒有什麼工作,沒有顯示調試中,enque schript將不會加載。

+2

嘗試在函數中添加is_page()代碼,並在諸如「wp_loaded」之類的鉤子中調用它。 – WisdmLabs 2014-10-31 12:43:10

回答

1

你試過嗎?

你只需要一個函數來排隊,然後你檢查,如果你的頁面是in是你想要排隊劇本的頁面。

I t眨眼你正在使用一個兒童主題,這個腳本(ledenlijst.js)是在你的孩子主題不在父主題,我是對嗎?如果是的話是回答你必須改變get_template_directory_uri()get_stylesheet_directory_uri()因爲get_template_directory_uri()(兒童主題的上下文時),它調用父主題目錄和get_stylesheet_directory_uri()指兒童主題目錄

function enqueue_child_theme_styles() { 
    wp_enqueue_style('parent-style', get_template_directory_uri().'/style.css'); 
    wp_enqueue_style('child-style', get_stylesheet_uri(), array('parent-style') ); 
    if(is_page(69)){ 
     wp_enqueue_script('your-script', get_template_directory_uri() . '-child/ledenlijst.js', array('jquery')); 
    } 
} 

資源:
get_template_directory_uri()
get_stylesheet_directory_uri()

對不起,我的英文不好

+0

我試過了,但沒有奏效。我做了什麼,是我移動了 \t add_action('wp_enqueue_scripts','add_ledenlijst');到頁面模板。現在它工作,但我認爲這不是一個非常整潔的解決方案。我認爲阿南德有一個點的情況下命令.. sugggestions更好的解決方案,不需要頁面模板,歡迎 – Rtenklooster 2014-10-31 14:14:19

+0

@Anand我知道'is_page'不能在functions.php上工作,但是當掛鉤到像'wp_enqueue_scripts'這樣的鉤子上的函數被掛鉤到'wp_head'上,並且在那一點上它已經知道哪個頁面已經被加載了。我只是不明白,爲什麼你會在這個代碼工作的時候低估它。 – 2014-10-31 14:21:24

+0

@ user2886088當您測試代碼時發生了什麼?任何錯誤?任何在控制檯上?這只是一個頁面?首頁?家? – 2014-10-31 14:23:01

0

感謝馬科斯, 確實有效。我再次嘗試,忘了更改get_template_directory_uri()。

此代碼:

function enqueue_child_theme_styles() { 
    wp_enqueue_style('parent-style', get_template_directory_uri().'/style.css'); 
    wp_enqueue_style('child-style', get_stylesheet_uri(), array('parent-style') ); 
    if(is_page(69)){ 
    wp_enqueue_script('your-script', get_template_directory_uri() . '-child/ledenlijst.js', array('jquery')); 
    } 
} 
add_action('wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX); 

無需自定義頁面/模板了。滿意的解決方案!

+0

=)樂於幫助。根據你的回答在這裏修改我的答案 – 2014-10-31 14:44:56