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;
}
}
我走到哪裏錯了