在基本分类URL上显示分类的所有帖子

时间:2011-09-03 作者:kel

我知道也有类似的答案,但我没有任何运气。

我有一个称为事件的自定义分类法。我想看看这里所有条款的所有帖子:域名。com/事件/

虽然它有404,但这不起作用。这在域中有效。com/事件/期限

我如何让它以另一种方式工作?

1 个回复
最合适的回答,由SO网友:Manny Fleurmond 整理而成

重拨此项。现在,它应该适用于任何和所有post\\u类型:

<?php
add_action(\'init\', \'setup_events_rewrite\');
//Gives you a new query var and sets up a rewrite rule that makes events/ work
function setup_events_rewrite() {
    //create a query var
    add_rewrite_tag(\'%tax_archive%\',\'([^/]+)\');
    //set new query vart with tax of choiice
    add_rewrite_rule( \'events/?$\', \'index.php?tax_archive=events\', \'top\' );  
}

add_filter(\'request\', \'setup_events_request\');
//sets the post_type is tax_archive is set to events
function setup_events_request($qv) {
    if(isset($qv[\'tax_archive\']) && $qv[\'tax_archive\'] == \'events\') {
        $qv[\'post_type\'] = \'any\';   //set post type to whatever you like here
    }

    return $qv;
}

add_filter( \'posts_clauses\', \'setup_events_post_clause\', 10, 2 );
//If the query var tax_archive is set and is equal to events, it\'ll change the query to pull only posts that have a term from the events taxonomy
function setup_events_post_clause($clauses, $wp_query) {
    global $wpdb;
    if(isset($wp_query->query[\'tax_archive\']) && \'events\' == $wp_query->query[\'tax_archive\']) {
        $clauses[\'join\'].= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;

        $clauses[\'where\'] .= $wpdb->prepare(" AND taxonomy = %s", $wp_query->query[\'tax_archive\']);
        $clauses[\'groupby\'] = "object_id";      
    }

    return $clauses;
}
?>

结束

相关推荐