2015-05-06 64 views
0

我正在开发一个插件。该插件使自定义帖子类型“产品”。首先,我使用template_redirect操作重定向到单页和分类页面。WordPress template_include过滤器无法正常工作

这里是我的template_redirect代码:

add_action("template_redirect", 'my_theme_redirect'); 
function my_theme_redirect() { 

    global $wp; 
    $plugindir = dirname(__FILE__); 
    //A Specific Custom Post Type 
    if ($wp->query_vars["post_type"] == 'product') { 
     $templatefilename = 'single-product.php'; 
     if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) { 
      $return_template = TEMPLATEPATH . '/' . $templatefilename; 
     } else { 
      $return_template = $plugindir . '/themefiles/' . $templatefilename; 
     } 
     do_theme_redirect($return_template); 
    } 
    if (is_tax('prodcategories')) { 
     $templatefilename = 'taxonomy-prodcategories.php'; 
     if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) { 
      $return_template = TEMPLATEPATH . '/' . $templatefilename; 
     } else { 
      $return_template = $plugindir . '/themefiles/' . $templatefilename; 
     } 
     do_theme_redirect($return_template); 
    } 
} 

function do_theme_redirect($url) { 
    global $post, $wp_query; 
    if (have_posts()) { 
     include($url); 
     die(); 
    } else { 
     $wp_query->is_404 = true; 
    } 
} 

其完美的工作。但现在我试图使用template_include过滤器,但它不工作我的网站变成空白。

这里是template_include代码:

add_filter("template_include", 'my_theme_redirect'); 
function my_theme_redirect($templatefilename) { 

    global $wp; 
    $plugindir = dirname(__FILE__); 
    //A Specific Custom Post Type 
    if ($wp->query_vars["post_type"] == 'product') { 
     $templatefilename = 'single-product.php'; 
     if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) { 
      $return_template = TEMPLATEPATH . '/' . $templatefilename; 
     } else { 
      $return_template = $plugindir . '/themefiles/' . $templatefilename; 
     } 
     return $return_template; 
    } 
    if (is_tax('prodcategories')) { 
     $templatefilename = 'taxonomy-prodcategories.php'; 
     if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) { 
      $return_template = TEMPLATEPATH . '/' . $templatefilename; 
     } else { 
      $return_template = $plugindir . '/themefiles/' . $templatefilename; 
     } 
     return $return_template; 
    } 
} 

function do_theme_redirect($url) { 
    global $post, $wp_query; 
    if (have_posts()) { 
     include($url); 
     die(); 
    } else { 
     $wp_query->is_404 = true; 
    } 
} 

我走到哪里错了

回答

0

好吧,我由我自己完成的任何建议。

我删除了上面的所有代码并编写了这段代码。它完美对我来说

代码:

function template_chooser($template){ 
    global $wp_query; 
    $plugindir = dirname(__FILE__); 

    $post_type = get_query_var('post_type'); 

    if($post_type == 'product'){ 
     return $plugindir . '/themefiles/single-product.php'; 
    } 

    if (is_tax('prodcategories')) { 
     return $plugindir . '/themefiles/taxonomy-prodcategories.php'; 
    } 

    return $template; 
} 
add_filter('template_include', 'template_chooser'); 
相关问题