doupao5296 2014-11-13 12:51
浏览 22
已采纳

创建2个Wordpress帖子类型模板

I have made a template for my post type, this works great. However I want similar duplication with certain parts of the original.

I have an ajax call using jQuery $.get, I want to target the second post type template to pull the html only into my current page.

At the moment the ajax call loads the entire page including the scripts. Modernizr gets loaded as does the entire content.

I have tried use a query var, like so:

// http://scratch99.com/wordpress/development/how-to-change-post-template-via-url-parameter/

function sjc_add_query_vars($vars) {
    return array('template') + $vars;
}
add_filter('query_vars', 'sjc_add_query_vars');

function sjc_template($template) {
  global $wp;
  if ($wp->query_vars['template'] === 'test') {
    return dirname( __FILE__ ) . '/single-test.php';
  }
  else {
    return $template;
  }
}
add_filter('single_template', 'sjc_template');

The code works well however I get this error

Notice: Undefined index: template in /wp-content/themes/custom--theme/functions.php on line 262

262 is: f ($wp->query_vars):

When the project single is loaded normally I believe this to be an issue when the the code hits the else statement.

Any help would be great.

  • 写回答

1条回答 默认 最新

  • douxie5930 2014-11-13 13:46
    关注

    You've get this error, because your $wp->query_vars is not exists, or if it exists, (than it is actually an array) the template key does not exist. What i did is to check, is the whole $wp->query_vars['template'] thing is exists with the PHP isset() function:

    From PHP documentation:
    isset — Determine if a variable is set and is not NULL
    

    Read my comments. This is the best explanation from me.

    function sjc_template($template) {
        //Now we have a $template variable with a value.
    
        //Get the $wp as a global variable
        global $wp;
    
        //This condition is check, is there an existing key in $wp->query_vars 
        //called template. If $wp->query_vars or $wp->query_vars['template'] not 
        //extists (the second can not exists if the first is not exists), 
        //then it will not check, what is the value is, instead of that just jump
        //to the second case to return $template;
        if (isset($wp->query_vars['template']) && $wp->query_vars['template'] === 'test') {
            //So at here, we have this: $wp->query_vars['template'] and the value of it is test
            return dirname(__FILE__) . '/single-test.php';
        } else {
            //In all other cases we return with the given parameter of function
            return $template;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)