WordPress template_include 过滤器无法正常工作 [英] WordPress template_include filter not working properly

查看:68
本文介绍了WordPress template_include 过滤器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件.该插件使自定义帖子类型产品".首先,我使用 template_redirect 动作重定向到单个页面和类别页面.

I am working on a plugin. The plugin make custom post type "product". First i use template_redirect action for redirection to single and category pages.

这是我的模板重定向代码:

Here is my template_redirect code:

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 过滤器,但它不起作用我的网站变为空白.

Its working perfectly. But now i am trying to use template_include filter but its not working my site goes blank.

这里是 template_include 代码:

Here is template_include Code:

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;
    }
}

我出错的任何建议

推荐答案

好的,我自己做的.

我删除了以上所有代码并编写了此代码.它非常适合我

I delete all above code and write this code. It works perfectly for me

代码:

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');

这篇关于WordPress template_include 过滤器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆