【问题标题】:Wordpress+ACF - Filtering through multiselect options in a repeater fieldWordpress+ACF - 通过转发器字段中的多选选项进行过滤
【发布时间】:2020-11-25 11:30:11
【问题描述】:

我正在将一种 $_POST 方法与 ajax 过滤集成在一起,以过滤所有帖子中的转发器。所以我没有过滤帖子(总是显示所有帖子),只是在每个帖子中过滤。

repeater 字段包含两个子字段——多选和所见即所得编辑器。 现在,根据用户在前端表单中选择的内容,如果它等于后多选值,它应该显示匹配多选的所见即所得编辑器字段。

我在没有多选的情况下工作。但是使用多选,我无法获得所有选定过滤器值的条件,以完全匹配所有多选值。 所以我得到的结果,因为它在一个 foreach 循环中,是多个所见即所得的编辑器字段。

我尝试了很多东西,这是其中之一的示例代码: (‘cond_options’ – 多选 '描述' - 所见即所得编辑器' “天气/天空/夜晚”——过滤值)

if ( have_rows('cond-repeater') ):
   while (have_rows('cond-repeater') ) : the_row();

        $select_options = get_sub_field('cond_options');
        $selectdesc = get_sub_field('description');

            if( $select_options ):
               foreach( $select_options as $select ):
                    if( isset( $_POST['weather'] ) && $_POST['weather'] && isset( $_POST['sky'] ) && $_POST['sky'] && isset( $_POST['night'] ) && $_POST['night'] == $select  ){
                        echo $selectdesc;
                    }  
            echo $select; //just to see the output of selected options
                 endforeach; 

            endif; 

    endwhile;
endif;

【问题讨论】:

  • 如果你想检查 all 你的条件是否为真,那么输出不属于 foreach 循环,但在它之后 - 只有这样你才能判断所有条件是否为真;当你在循环中时,你甚至还不知道。使用布尔标志,在循环之前用 true 初始化,如果任何条件不匹配,则将其设置为 false,然后跳出循环。在循环之后根据该标志的值处理输出。
  • 您的多选 acf 字段中有哪些值?

标签: php wordpress filtering advanced-custom-fields ajaxform


【解决方案1】:

在不了解您的数据和您试图实现的实际最终结果的情况下。我整理了一些可能有助于引导您朝着正确方向前进的东西。

主要要指出的是in_array的使用,通过它我们可以检查一个值是否存在于多选中,而不必用foreach循环遍历它。

如果这有帮助,请告诉我。

$_weather = !empty( $_POST[ 'weather' ] ) ? $_POST[ 'weather' ] : null;
$_sky = !empty( $_POST[ 'sky' ] ) ? $_POST[ 'sky' ] : null;
$_night = !empty( $_POST[ 'night' ] ) ? $_POST[ 'night' ] : null;

if ( have_rows('cond-repeater') ) {
    while ( have_rows('cond-repeater') ) {
        the_row();

        if ( $options = get_sub_field( 'cond_options' ) ) {

            // Check payload includes all required parameters
            if ( $_weather && $_sky && $_night ) {

                // Check if all parameters exist in the multiselect value
                if ( in_array( $_weather, $options ) && in_array( $_sky, $options ) && in_array( $_night, $options ) ) {
                    echo get_sub_field( 'description' );
                } else {
                    echo $select; // Warning: Undefined variable!
                }
            }
        }

    }
}

【讨论】:

  • 非常感谢,我在使用 in_array 之前尝试过,但会再次与您的基地核对并通知您。谢谢!
猜你喜欢
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多