自动为多个分类生成重写规则

时间:2012-03-18 作者:helgatheviking

所以我回来重写。。。我正在寻找更自动化的解决方案。

我在互联网上偶然发现了以下内容:

/**
 * Generates all the rewrite rules for a given post type.
 *
 * The rewrite rules allow a post type to be filtered by all possible combinations & permutations
 * of taxonomies that apply to the specified post type and additional query_vars specified with
 * the $query_vars parameter.
 *
 * Must be called from a function hooked to the \'generate_rewrite_rules\' action so that the global
 * $wp_rewrite->preg_index function returns the correct value.
 *
 * @param string|object $post_type The post type for which you wish to create the rewrite rules
 * @param array $query_vars optional Non-taxonomy query vars you wish to create rewrite rules for. Rules will be created to capture any single string for the query_var, that is, a rule of the form \'/query_var/(.+)/\'
 *
 * @author Brent Shepherd <[email protected]>
 * @since 1.0
 */

function eg_generate_rewrite_rules( $post_type, $query_vars = array() ) {
    global $wp_rewrite;

    if( ! is_object( $post_type ) )
        $post_type = get_post_type_object( $post_type );

    $new_rewrite_rules = array();

    $taxonomies = get_object_taxonomies( $post_type->name, \'objects\' );   

    // Add taxonomy filters to the query vars array
    foreach( $taxonomies as $taxonomy ){
        //$query_vars[$taxonomy->rewrite[\'slug\']] = $taxonomy->query_var;
        $query_vars[] = $taxonomy->rewrite[\'slug\'] ;
        add_rewrite_tag(\'%\'.$taxonomy->rewrite[\'slug\'].\'%\',\'([^&]+)\');
    }

    // Loop over all the possible combinations of the query vars
    for( $i = 1; $i <= count( $query_vars );  $i++ ) {

        $new_rewrite_rule =  $post_type->rewrite[\'slug\'] . \'/\';
        $new_query_string = \'index.php?post_type=\' . $post_type->name;

        // Prepend the rewrites & queries
        for( $n = 1; $n <= $i; $n++ ) {
            $new_rewrite_rule .= \'(\' . implode( \'|\', $query_vars ) . \')/(.+?)/\';
            $new_query_string .= \'&\' . $wp_rewrite->preg_index( $n * 2 - 1 ) . \'=\' . $wp_rewrite->preg_index( $n * 2 );
        }

        // Allow paging of filtered post type - WordPress expects \'page\' in the URL but uses \'paged\' in the query string so paging doesn\'t fit into our regex
        $new_paged_rewrite_rule = $new_rewrite_rule . \'page/([0-9]{1,})/\';
        $new_paged_query_string = $new_query_string . \'&paged=\' . $wp_rewrite->preg_index( $i * 2 + 1 );

        // Make the trailing backslash optional
        $new_paged_rewrite_rule = $new_paged_rewrite_rule . \'?$\';
        $new_rewrite_rule = $new_rewrite_rule . \'?$\';

        // Add the new rewrites
        $new_rewrite_rules = array( $new_paged_rewrite_rule => $new_paged_query_string,
                                    $new_rewrite_rule       => $new_query_string )
                             + $new_rewrite_rules;
    }

    return $new_rewrite_rules;
}



// add the rules
function mv_add_rewrite_rule($wp_rewrite){
    $new_rules = eg_generate_rewrite_rules(\'product\');
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action(\'generate_rewrite_rules\',\'mv_add_rewrite_rule\',99);

http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/

但是,如果没有为每个自定义分类法设置重写段塞,这会很好,但如果重写段塞与查询变量不同,则会失败。(因为它会自动添加$match中捕获的query\\u var作为查询字符串。)。

如何修改此函数,使其与重写[\'slug\']匹配,但将关联的query\\u var添加到查询字符串中。

呸。。。我希望这是有意义的。或者,这听起来可能吗?是否建议改为硬编码重写规则?

产品类别/面料/颜色/红色

产品标签/新款/款式/提花

(产品类别或产品标签)/(颜色或样式或材料装饰样式)

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

当我发现自己再次在谷歌上搜索重写时,一年后我将发布教程作者提供给我的解决方案。解决方案是设置所需的query_var 注册分类时。

register_taxonomy( \'product_cat\', \'product\', array( \'query_var\' => \'product-category\' ) );

结束

相关推荐