自定义帖子类型单页返回列表页面

时间:2015-08-10 07:33:58

标签: php wordpress wordpress-plugin

我正在开发一个插件。在插件中,我创建了在插件激活后移动到主题文件夹中的文件。文件在活动主题中成功移动。

我使用waitForElement("yourId",function(){ console.log("done"); }); 来包含主题中的文件。我创建了一个页面模板文件,用于列出页面' my_items.php'并存档页面' archive-my_items.php'对于标签页面' taxonomy-my_tags.php'并且对于单个&my-my_items.php'。除了' single-my_itmes.php'之外,所有文件都能正常运行。当我点击我的项目页面时,列表页面打开,当我点击标签时,标签文件打开,但当我点击查看更多从列表页面再次重定向列表页面时,地址栏上的链接是单页链接但它访问列表页面。

这是我的代码:

template_include

当我评论我的代码时:

add_filter('template_include', 'foo_template_chooser');
function foo_template_chooser($template){
    global $wp;
    $plugindir = dirname(__FILE__);

    if( $wp->query_vars["post_type"] == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/archive-my_items.php')) {
            return locate_template('my_items/archive-my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }

        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }
    }

    if( $wp->query_vars["post_type"] == 'my_items' && is_single() ){
        if(file_exists(TEMPLATEPATH . '/my_items/single-my_items.php')) {
            return locate_template('my_items/single-my_items.php');
        } else {
            return $plugindir . '/template/single-my_items.php';
        }
    }

    if ( is_tax('my_tags') ) {
        if(file_exists(TEMPLATEPATH . '/my_items/taxonomy-my_tags.php')) {
            return locate_template('my_items/taxonomy-my_tags.php');
        } else {
            return $plugindir . '/template/taxonomy-my_tags.php';
        }
    }

    return $template;   
}

然后单页工作但列表页面显示WordPress默认布局。那么我该怎么做才能引导我。

1 个答案:

答案 0 :(得分:0)

好的,我自己得到了答案

我只是将单个文件条件放在文件模板上面

这是我的代码:

add_filter('template_include', 'foo_template_chooser');
function foo_template_chooser($template){
    global $wp;
    $plugindir = dirname(__FILE__);

    if( $wp->query_vars["post_type"] == 'my_items' && is_single() ){
        if(file_exists(TEMPLATEPATH . '/my_items/single-my_items.php')) {
            return locate_template('my_items/single-my_items.php');
        } else {
            return $plugindir . '/template/single-my_items.php';
        }
    }

    if( $wp->query_vars["post_type"] == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/archive-my_items.php')) {
            return locate_template('my_items/archive-my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }

        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }
    }

    if ( is_tax('my_tags') ) {
        if(file_exists(TEMPLATEPATH . '/my_items/taxonomy-my_tags.php')) {
            return locate_template('my_items/taxonomy-my_tags.php');
        } else {
            return $plugindir . '/template/taxonomy-my_tags.php';
        }
    }

    return $template;   
}

它的工作完美

相关问题