最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - WordPress template_include filter not working properly

programmeradmin4浏览0评论

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

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

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

Any suggestions where i go wrong

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

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

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

Any suggestions where i go wrong

Share Improve this question asked May 6, 2015 at 7:04 deemi-D-nadeemdeemi-D-nadeem 5672 gold badges8 silver badges26 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

Ok i done by my own.

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

Code:

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

This is more safeway.

add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template');


function wpse138858_woocommerce_category_archive_template( $original_template ) {
    // we're loading the template conditionally, 


  global $post;
   if (is_archive() && get_post_type($post)=='ym_package') {
        // you've to create the template you want to use here first
        return get_template_directory().'/archive-ym_package.php';
    } 
    else if(is_archive() && get_post_type($post)=='ym_place') {
        // you've to create the template you want to use here first
        return get_template_directory().'/archive-ym_place.php';
    } 


    else {
        return $original_template;
    }
}

Please note that put this code in function.php to works fine.

This work for me, kindly try it, Thanks
Templates is loaded into cpt file, which was located at
custom_plugin -> app -> cpt -> cpt_article.php
Template is located
custom_plugin -> app -> templates

add_filter( 'template_include', 'load_my_custom_template', 99, 1 );
 function load_custom_templates($template) {
   $post_type = 'article';
   if( is_post_type_archive( $post_type ) && file_exists( plugin_dir_path(__FILE__) . 
      'templates/archive_help_lessions.php' ) ){
      $template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'archive_articles.php';
   }
   if ( is_singular( $post_type ) ) {
      $template = trailingslashit( plugin_dir_path( __FILE__ ) .'app/templates' ).'single_article.php';
  }
   return $template;
}
发布评论

评论列表(0)

  1. 暂无评论