首页 文章

高级自定义字段 - 与自定义分类法匹配的自定义位置规则

提问于
浏览
0

我在WordPress网站上使用Advanced Custom Fields plugin . 我的目标是有一个自定义位置规则,以便在选择自定义分类中的任何术语时显示自定义归档组 .

在ACF站点上的custom location rule tutorial之后,我将自定义类型,运算符和值添加到规则行 .

我的匹配函数(下面的代码)仅在重新加载页面时有效,但不能通过AJAX . 如何在$ options数组中添加自定义分类,以便在选中/取消选中自定义分类术语时,匹配函数可以通过AJAX进行评估 .

function acf_location_rules_match_taxonomyTerm( $match, $rule, $options ){
    // vars
    $taxonomies = get_object_taxonomies( $options['post_type'] );
    $terms = $options['taxonomy'];
    // not AJAX
    if( !$options['ajax'] ){
        // no terms? Load them from the post_id
        if( empty($terms) ){
            if( is_array($taxonomies) ){
                foreach( $taxonomies as $tax ){
                    $all_terms = get_the_terms( $options['post_id'], $tax );
                    if($all_terms){
                        foreach($all_terms as $all_term){
                            $terms[] = $all_term->term_id;
                        }
                    }
                }
            }
        }
        if($rule['operator'] == "<==>"){
            $match = false;
            if($terms){
                $current_terms = get_the_terms($options['post_id'], $rule['value']);
                if ( $current_terms && ! is_wp_error( $terms ) ) {
                    $current_term_ids = array();
                    foreach ($current_terms as $current_term) {
                        $current_term_ids[] = $current_term->term_id;
                    }
                }
                foreach ($current_term_ids as $current_term_id) {
                    if( in_array($current_term_id, $terms) ){
                        $match = true;
                    }
                }
            }
        }
        else{
            $match = false;
        }
    }
    return $match;
}

1 回答

相关问题