Wordpress 更多分类筛选:如何显示结果?

Wordpress 更多分类筛选:如何显示结果?,wordpress,woocommerce,Wordpress,Woocommerce,我有两种自定义分类法 我想过滤我的产品,获得与此wp_查询相同的结果 $args = array( 'posts_per_page' => -1, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'auto', 'field' => 'ID', 'terms' =>

我有两种自定义分类法

我想过滤我的产品,获得与此wp_查询相同的结果

$args = array(
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'auto',
            'field' => 'ID',
            'terms' => $auto_id
        ),
        array(
            'taxonomy' => 'ricambio',
            'field' => 'ID',
            'terms' => $ricambi_id
        )
    ),
    'post_type' => 'product',
    'orderby' => 'title',
);
$query = new WP_Query( $args );
如何显示此筛选结果? 我必须创建一个像taxonomy-auto-ricambio.php这样的模板?存档自动李嘉图

如果我在我的url地址中这样写:

http://www.myshop.com/?auto=my_auto&ricambio=my_ricambio 
它起作用了

如何在具有漂亮url的模板中显示它


谢谢。

这篇优秀的文章直接引自了本网站

首先创建一些新的重写规则。这可能应该在站点特定的代码片段插件中,而不是在您的主题中。然后转到永久链接并重新保存永久链接

function so_25722819_add_rewrite_rules() {
    global $wp_rewrite;

    $new_rules = array(
        'event/(industry|location)/(.+?)/(industry|location)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
        'event/(industry|location)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'so_25722819_add_rewrite_rules' );
现在,如果您使用url访问您的站点:

www.example.com/auto/my_auto/ricambio/my_ricambio

您应该转到一个分类法归档页面,该页面显示与以下相同的结果:

www.example.com/?auto=my\u auto&ricambio=my\u ricambio

为了呼应这些漂亮的永久链接,你可以使用文章作者提供的插件,只需稍加调整即可从他的
eg_events
post类型切换到你的
product
post类型

function eg_get_filter_permalink( $taxonomy_slug, $term ) {
    global $wp_query;

    // If there is already a filter running for this taxonomy
    if( isset( $wp_query->query_vars[$taxonomy_slug] ) ){
        // And the term for this URL is not already being used to filter the taxonomy
        if( strpos( $wp_query->query_vars[$taxonomy_slug], $term ) === false ) {
            // Append the term
            $filter_query = $taxonomy_slug . '/' . $wp_query->query_vars[$taxonomy_slug] . '+' . $term;
        } else {
            // Otherwise, remove the term
            if( $wp_query->query_vars[$taxonomy_slug] == $term ) {
                $filter_query = '';
            } else {
                $filter = str_replace( $term, '', $wp_query->query_vars[$taxonomy_slug] );
                // Remove any residual + symbols left behind
                $filter = str_replace( '++', '+', $filter );
                $filter = preg_replace( '/(^\+|\+$)/', '', $filter );
                $filter_query = $taxonomy_slug . '/' . $filter;
            }
        }
    } else {
        $filter_query = $taxonomy_slug . '/' . $term;
    }

    // Maintain the filters for other taxonomies
    if( isset( $wp_query->tax_query ) ) {

        foreach( $wp_query->tax_query->queries as $query ) {

            $tax = get_taxonomy( $query['taxonomy'] );

            // Have we already handled this taxonomy?
            if( $tax->query_var == $taxonomy_slug )
                continue;

            // Make sure taxonomy hasn't already been added to query string
            if( strpos( $existing_query, $tax->query_var ) === false )
                $existing_query .= $tax->query_var . '/' . $wp_query->query_vars[$tax->query_var] . '/';
        }

    }

    if( isset( $existing_query ) )
        $filter_query = $existing_query . $filter_query;

    return trailingslashit( get_post_type_archive_link( 'product' ) . $filter_query );
}
然后,在任何模板中,您都可以使用上述函数为您的多分类法归档生成一个非常好的永久链接:

// Link to page with only my_ricambio in my_auto 
echo eg_get_filter_permalink( 'my_auto', 'my_ricambio' );