2015-05-06 61 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'); 
相關問題