【问题标题】:Checking for page templates in child theme检查子主题中的页面模板
【发布时间】:2013-03-13 09:59:09
【问题描述】:

我有一个简单的问题,希望有人能解释一下。我有一个带有自定义页面模板的子主题,我只是想检查天气是否正在使用该模板。在正常情况下,我只会使用 is_page_template 函数,但它似乎不适用于子主题。我已经尝试了以下

if(is_page_template('template-custom-fullwidth.php')){
    //do something..
}

还有

if(is_page_template(dirname(get_bloginfo('stylesheet_url')).'/template-custom-fullwidth.php'){
    //do something..
}

Neiter 有效,我希望有一个比使用$_SERVER 检查 URL 更优雅的解决方案。我无法想象没有这样的功能,因为这似乎是一项常见的任务。我相信问题是模板和样式表目录之间的区别。是否可以使用 Wordpress 来检查位于子主题中的页面模板?

提前致谢!

【问题讨论】:

  • 你在哪里使用这个代码?
  • @MidhuN - 我实际上是在我的子主题中使用 functions.php 文件中的代码,我试图尽可能避免修改父主题,但我认为这就是问题是。
  • 你不是从循环中调用的函数中调用它,是吗?你这样做是为了处理页面(在 WP 意义上,即相对静态的内容),对吧?

标签: php wordpress templates parent-child


【解决方案1】:

我遇到了类似的问题。一些试验和错误发现,如果条件保留在函数内部,它就可以工作。但如果放在外面就不会。

function team_page_enqueue_style() {

    if ( is_page_template('page-team.php') ) {
        wp_enqueue_style( 'easy-responsive-tabs-css', get_stylesheet_directory_uri() . '/css/easy-responsive-tabs.css', array(), NULL);
    }
}

add_action( 'wp_enqueue_scripts', 'team_page_enqueue_style' );

【讨论】:

    【解决方案2】:

    如果在循环中使用了 is_page_template,请尝试在调用 is_page_template 之前放置 wp_reset_query()

    【讨论】:

      【解决方案3】:

      遇到同样的问题并这样解决:

      get_stylesheet_directory_uri() 不起作用,因为它会显示 url,并且您需要服务器完整路径,请使用 get_stylesheet_directory()

      如果is_page_template() 不起作用,您可以使用get_page_template() 并进行比较

      if(get_page_template() == (get_stylesheet_directory() . '/custom-template.php')){
      //your stuff
      }
      

      【讨论】:

        【解决方案4】:

        试试is_singular(); 它适用于我的情况,因为我的模板是单个帖子页面模板。

        要使用它,您需要指定不带single-.php 字样的模板名称。例如,如果模板文件是single-forest_of_trees.php,那么这应该是代码:

        if (is_singular( 'forest_of_trees' )) {
           // do something
        }
        

        它还允许以优雅的方式处理多个值。

        【讨论】:

          【解决方案5】:

          对我有用的是(在functions.php中):

          if (get_page_template_slug() == 'template-custom-fullwidth.php'){
          
          // anything
          
          }
          

          【讨论】:

            【解决方案6】:

            在子主题中唯一对我有用的是访问全局 $template 变量并与它进行比较:

            global $template;
            if (basename($template) === 'template-custom-fullwidth.php') {
               // do something
            }
            

            【讨论】:

              【解决方案7】:

              使用主题文件夹中的相对路径:

              if(is_page_template('page-templates/template-custom-fullwidth.php')){
              //do something..
              }
              

              【讨论】:

                【解决方案8】:

                参考这个 WP Codex 页面:http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

                请改用get_stylesheet_directory_uri。这应该有效:

                is_page_template(get_stylesheet_directory_uri() . '/template-custom-fullwidth.php')
                {
                    //do something..
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-08-31
                  • 1970-01-01
                  • 2023-03-03
                  • 1970-01-01
                  相关资源
                  最近更新 更多