我使用下面的函数来调用一个自定义的单个帖子,覆盖默认的single.php
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');发布于 2017-01-12 22:19:36
有一个类似的帖子,但它并没有完全涵盖你的请求。这里是链接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
} ?>它不像你想要的那样干净,但我认为它会对你有帮助。
https://stackoverflow.com/questions/41607002
复制相似问题