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');
你是否在* single-1.php *文件中顯示任何自定義'post_type'? –
嘗試使用'is_singular('you_custom_post_type');' - 參考 - https://codex.wordpress.org/Function_Reference/is_singular – htmlbrewery
不,它不是一個自定義帖子類型,也不適用於任何自定義帖子類型。正如你所看到的文件single-1.php位於/ includes/templates文件夾,並且從那裏我調用這個來覆蓋默認的single.php文件,這個過濾器single_template。如果我應用這個,默認博客single.php將被替換爲single-1.php。但不確定如何有條件地檢查這個, – Ayanize