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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据