首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在WordPress中有条件地检查自定义单个帖子模板

如何在WordPress中有条件地检查自定义单个帖子模板
EN

Stack Overflow用户
提问于 2017-01-12 15:02:00
回答 1查看 1.2K关注 0票数 0

我使用下面的函数来调用一个自定义的单个帖子,覆盖默认的single.php

代码语言:javascript
复制
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文件,比如

代码语言:javascript
复制
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');
EN

Stack Overflow用户

回答已采纳

发布于 2017-01-12 22:19:36

有一个类似的帖子,但它并没有完全涵盖你的请求。这里是链接here

下面是有价值的代码:

代码语言:javascript
复制
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文件,下面是一个示例:

代码语言:javascript
复制
<?php if('single-1.php' == get_current_template())
    {
        //load your scripts here
    } ?>

它不像你想要的那样干净,但我认为它会对你有帮助。

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41607002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档