php facetwp将facet术语选项与分类法归档页面的层次结构相匹配

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php facetwp将facet术语选项与分类法归档页面的层次结构相匹配相关的知识,希望对你有一定的参考价值。

<?php

/** on a taxonomy term page remove terms from facet that are not the same or a sub category of that term
 * example is for woocommerce product categories
 */
add_filter( 'facetwp_facet_render_args', function( $args ) {
    if ( 'product_categories' == $args['facet']['name'] && is_tax( 'product_cat' ) ) {
        $current_term = get_queried_object_id();
        if ( ! empty( $args['values'] ) ) {
            foreach ( $args['values'] as $key => $val ) {
                $term_id = $val['term_id'];
                // this keeps sub-terms of the current term archive page
                if ( $val['parent_id'] && in_array( $current_term, get_ancestors( $val['term_id'], 'product_cat', 'taxonomy' ) ) ) {
                    continue;
                // this keeps term for current archive page, delete this else if to if not wanting the current term also
                } else if ( $current_term == $val['term_id'] ) {
                        continue;
                // this removes non-matching terms from the choices for the facet.
                } else {
                    unset( $args['values'][$key] );
                }
            }
            $args['values'] = array_values( $args['values'] );
        }
    }
    return $args;
});

以上是关于php facetwp将facet术语选项与分类法归档页面的层次结构相匹配的主要内容,如果未能解决你的问题,请参考以下文章

php facetwp facetwp_facet_filter_posts钩子

php facetwp用facet的map facet替换listify map

php facetwp修改facet计数

php facetwp在autoselect上重置facet

php FacetWP - 类别重定向到排序的facets存档

php facetwp用facet sort替换woocommerce排序