未设置术语时的自定义分类列表页面(所有术语)

时间:2010-11-29 作者:Phil Brown

我创建了一个自定义分类法“foo”,其中包含重写slug“foo”和query\\u var“foo”。

例如,我在“foo”分类法中也有“bar”和“baz”这两个术语。

目前,permalinks喜欢/foo/bar/foo/baz 使用我的taxonomy-foo.php 样板

我想做的是能够/foo 并检索所有带有指向任何“foo”分类法链接的帖子。当前,此URL显示404。

我怎样才能做到这一点?如果我能以某种方式连接到重写代码并设置默认的术语值(\'all\' 例如)如果缺失,这可能是一种前进的方式,但我不确定这是否可行。

我最好使用taxonomy-foo.php “catch all”的模板文件,处理显示来自其中一个或所有术语的帖子的逻辑。

干杯

Edit: 我已经想出了如何抓住/foo 并使用add_filter(\'request\', ...) 虽然我必须使用现有术语,因为使用其他任何术语都会生成404。有没有办法避免这种限制?

2 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

你想要的是an index page for custom taxonomies. 有a ticket for that, 但目前还不清楚在这一页上最明显的是什么:1)该分类法任何术语的所有帖子列表,或2)该分类法所有术语的列表?记住这一点category 也是一种分类法,但假设所有帖子都属于至少一个类别(带有Uncategorized 默认设置)。在这种情况下,选项1将与常规post查询相同,选项2可能更有意义。

自定义帖子类型索引页的相关问题will be fixed in the upcoming 3.1 release.

我自己尝试了一些东西,而不是干涉request hook,我认为更好的方法是添加新的重写规则并修改查询:

add_action( \'init\', \'wpse4663_init\' );
function wpse4663_init()
{
    // The custom taxonomy rewrite rules end up at the top of the rewrite array
    register_taxonomy(
        \'wpse4663\',
        array( \'post\' ),
        array(
            \'label\' => \'WPSE 4663\',
        )
    );

    // So there probably is no danger in adding this specific rewrite rule there too
    // We re-use the existing `taxonomy` query var, but with no `term`
    // We clean this up in `parse_query`, otherwise get_posts() complains
    // You could make this work for all taxonomies (categories, tags, ...) by doing this repeatedly
    // But then it\'s probably better to do this in the `rewrite_rules_array` filter
    add_rewrite_rule( \'wpse4663(/page/([0-9]+))?/?$\', \'index.php?taxonomy=wpse4663&paged=$matches[2]\', \'top\' );
}

add_filter( \'parse_query\', \'wpse4663_parse_query\' );
function wpse4663_parse_query( &$wp_query )
{
    // is_tax is only true if both a taxonomy and a term are set, otherwise is_home is true
    // But we don\'t want that: is_tax and is_archive should be true, is_home should be false
    if ( !$wp_query->is_tax && $taxonomy_query = $wp_query->get( \'taxonomy\' ) ) {
        foreach ( $GLOBALS[\'wp_taxonomies\'] as $taxonomy => $t ) {
            if ( $t->query_var && $taxonomy_query == $t->query_var ) {
                // Make sure the conditional tags work, so the right template is loaded
                $wp_query->is_tax = true;
                $wp_query->is_archive = true;
                $wp_query->is_home = false;

                // Make is_tax($taxonomy) work
                $wp_query->queried_object = $t;
                $wp_query->queried_object->taxonomy = $taxonomy;

                // If term is null, get_terms (query.php line 2043) will get all terms of this taxonomy
                $wp_query->set( \'term\', null );
                break;
            }
        }
    }
}

SO网友:Vivek Gounder

尽管上述答案适用于wordpress中的默认设置。如果你正在为你的分类法重写鼻涕虫的名字,那么简·法布里的答案就行不通了。它只需要修改一下。

此外,我建议不要这样做,因为这将不适合您可能正在使用的其他插件,这些插件不知道您正在使用索引页进行分类。它将开始抛出未定义的通知和其他错误。

add_action( \'init\', \'wpse4663_init\' );
function wpse4663_init()
{
    // The custom taxonomy rewrite rules end up at the top of the rewrite array
    register_taxonomy(
        \'wpse4663\',
        array( \'post\' ),
        array(
            \'label\' => \'WPSE 4663\',
        )
    );

    // So there probably is no danger in adding this specific rewrite rule there too
    // We re-use the existing `taxonomy` query var, but with no `term`
    // We clean this up in `parse_query`, otherwise get_posts() complains
    // You could make this work for all taxonomies (categories, tags, ...) by doing this repeatedly
    // But then it\'s probably better to do this in the `rewrite_rules_array` filter
    add_rewrite_rule( \'wpse4663(/page/([0-9]+))?/?$\', \'index.php?taxonomy=wpse4663&paged=$matches[2]\', \'top\' );
}

add_filter( \'parse_query\', \'wpse4663_parse_query\' );
function wpse4663_parse_query( &$wp_query )
{
    // is_tax is only true if both a taxonomy and a term are set, otherwise is_home is true
    // But we don\'t want that: is_tax and is_archive should be true, is_home should be false
    if ( !$wp_query->is_tax && $taxonomy_query = $wp_query->get( \'taxonomy\' ) )       {
        foreach ( $GLOBALS[\'wp_taxonomies\'] as $taxonomy => $t ) {
            if($t->rewrite && isset($t->rewrite[\'slug\']){
                $slug = $t->rewrite[\'slug\'];
            }
            else{
                $slug = $t->query_var;
            }

            if ( $t->query_var && $taxonomy_query == $slug ) {
                // Make sure the conditional tags work, so the right template is loaded
                $wp_query->is_tax = true;
                $wp_query->is_archive = true;
                $wp_query->is_home = false;

                // Make is_tax($taxonomy) work
                $wp_query->queried_object = $t;
                $wp_query->queried_object->taxonomy = $taxonomy;

                // If term is null, get_terms (query.php line 2043) will get all terms of this taxonomy
                $wp_query->set( \'term\', null );
                break;
            }
        }
    }
}

结束

相关推荐