2017-01-12 40 views
0

定製的單篇文章模板,我使用下面的函數調用自定義的單個職位覆蓋默認的一個single.php如何有條件地檢查在WordPress

function call_different_single_post($single_template) 
{ 
    global $post; 

    $path = get_stylesheet_directory() . '/includes/templates' . '/'; 
    $single_template = $path. 'single-1.php'; 
    return $single_template; 
} 

add_filter('single_template', 'call_different_single_post'); 

它呼籲單個職位single-1.php模板。

現在我想有條件地檢查這個模板,這樣我可以調用其他一些js文件就像

function call_cust_js_single(){ 

    if(/*..this is single-1.php template..*/){ 
    wp_register_script('cust-js', get_stylesheet_directory_uri() . 'custom.js', false, '1.0.0'); 
    wp_enqueue_script('cust-js'); 

    } 

} 
add_action('wp_enqueue_scripts', 'call_cust_js_single'); 
+0

你是否在* single-1.php *文件中顯示任何自定義'post_type'? –

+0

嘗試使用'is_singular('you_custom_post_type');' - 參考 - https://codex.wordpress.org/Function_Reference/is_singular – htmlbrewery

+0

不,它不是一個自定義帖子類型,也不適用於任何自定義帖子類型。正如你所看到的文件single-1.php位於/ includes/templates文件夾,並且從那裏我調用這個來覆蓋默認的single.php文件,這個過濾器single_template。如果我應用這個,默認博客single.php將被替換爲single-1.php。但不確定如何有條件地檢查這個, – Ayanize

回答

1

有一個類似的職位,但它並不完全覆蓋你的要求。這裏的鏈接here

這裏的寶貴代碼:

add_filter('template_include', 'var_template_include', 1000); 
function var_template_include($t){ 
    $GLOBALS['current_theme_template'] = basename($t); 
    return $t; 
} 

function get_current_template($echo = false) { 
    if(!isset($GLOBALS['current_theme_template'])) 
     return false; 
    if($echo) 
     echo $GLOBALS['current_theme_template']; 
    else 
     return $GLOBALS['current_theme_template']; 
} 

現在你有功能get_current_template將返回該模板文件的文件名。

現在編輯你的header.php文件,這裏有一個例子:

<?php if('single-1.php' == get_current_template()) 
    { 
     //load your scripts here 
    } ?> 

它並不像你正在尋找乾淨,但我認爲它會幫助你。