自定义支柱类型模板插入不能正常工作

时间:2013-09-13 作者:jogesh_pi

我试图通过处理自定义帖子类型的插件插入自定义模板文件。除了包含部分的模板外,插件的其余部分都工作得很好。

class ClassName{

    public function __construct() {
        add_action( \'template_redirect\', array( $this, \'uniThemeRedirect\') );
    }


    public function uniThemeRedirect(){

        global $wp_query;
        global $wp;

        // Specific Custom Post Type
        if( $wp->query_vars["post_type"] == "videos" ){
            $templateFilename = "single-videos.php";

            if( file_exists( TEMPLATEPATH . \'/\' . $templateFilename ) ){
                $return_template = TEMPLATEPATH . \'/\' . $templateFilename;
            }
            else {
                $return_template = UNISLIDER_TEMPLATES . $templateFilename;
            }

            $this->doThemeRedirect( $return_template );
        } else {
            echo \'not working\';
        }

    }


    public function doThemeRedirect( $template_url ){
        global $post, $wp_query;
        if( have_posts() ){
            echo $template_url;
            require_once ( $template_url );
        }
        else{
            $wp_query->is_404 = true;
        }

    }

}

$handle = new ClassName();
我正在关注这个https://stackoverflow.com/questions/4647604/wp-use-file-in-plugin-directory-as-custom-page-template#answer-4975004 实例

但是当我在http://www.domain.com/videos 其中视频是我的自定义帖子类型。

我明白“不工作”意味着没有模板包含
Can anyone please help me what wrong i have done??

UPDATE: This is how i register the Custom Post Type

$labels = array(
             \'name\'=>__(\'UniVideos\', \'unislider\'), 
             \'singular_name\'=>__( \'Videos\', \'unislider\' ), 
             \'add_new\'=>__( \'Add New\', \'unislider\' ), 
             \'add_new_item\'=>__( \'Add New Video\' ), 
             \'edit_item\'=>__(\'Edit Video\'), 
             \'new_item\'=>__(\'New Video\'), 
             \'view_item\'=>__(\'View Videos Images\'), 
             \'search_item\'=>__(\'Search Slide\'), 
             \'not_found\'=>__(\'Noting Found\'), 
             \'not_found_in_trash\'=>__(\'Nothing found in Trash\'), 
             \'parent_item_colon\'=>\'\'
         );

         $args = array(
             \'labels\'=>$labels, 
             \'public\'=>true, 
             \'has_archive\'=>true, 
             \'publicly_queryable\'=>true, 
             \'show_ui\'=>true, 
             \'query_var\'=>true, 
             \'menu_icon\'=>\'\', 
             \'rewrite\'=>true, 
             \'capability_type\'=>\'post\', 
             \'hierarchical\'=>false, 
             \'menu_position\'=>NULL, 
             \'supports\'=>array( \'title\' )
         );

         register_post_type( \'videos\', $args );

1 个回复
SO网友:gmazzap

Short answer: 我想你的代码的问题是die(); 作为最后一行doThemeRedirect 方法

之后,您的代码可以大大简化,使用template_include 过滤器挂钩,而不是template_redirect 行动挂钩。

简化意味着减少出现错误的机会,或者至少让对错误的识别更简单。

 public function __construct() {
    add_filter( \'template_include\', array( $this, \'uniThemeRedirect\') );
}


public function uniThemeRedirect( $template ){
    if(  get_query_var(\'post_type\') == \'videos\' || (is_single() && get_post_type() == \'videos\') ) {

        // the \'single-videos.php\' will be included for both
        // single and archive view of CPT \'videos\'
        // to stop including for single view, remove
        // (is_single() && get_post_type() == \'videos\') ) from if condition
        // to stop including for archive view, remove
        // get_query_var(\'post_type\') == \'videos\' from if condition

        $templateFilename = \'single-videos.php\';
        $locate = locate_template( $templateFilename );
        if( $locate ) return $locate;
        if( file_exists( user_trailingslashit(UNISLIDER_TEMPLATES) . $templateFilename ) )
           return user_trailingslashit(UNISLIDER_TEMPLATES) . $templateFilename );
    }
    // die \'not working\';
    return $template;

}
所以你don\'t 需要doThemeRedirect 方法。

这段代码比您的更简单,但功能更强大(它支持您的子主题,而您的子主题则不支持),而且更实用:其中没有一个全局变量。

结束

相关推荐