簡體   English   中英

Wordpress+ACF - 通過轉發器字段中的多選選項進行過濾

[英]Wordpress+ACF - Filtering through multiselect options in a repeater field

我正在將一種 $_POST 方法與 ajax 過濾集成在一起,以過濾所有帖子中的轉發器。 所以我沒有過濾帖子(總是顯示所有帖子),只是在每個帖子中過濾。

轉發器字段包含兩個子字段——多選和所見即所得編輯器。 現在,根據用戶在前端表單中選擇的內容,如果它等於后多選值,它應該顯示匹配多選的所見即所得編輯器字段。

我有這個沒有多選的工作。 但是使用多選,我無法獲得所有選定過濾器值的條件,以完全匹配所有多選值。 所以我得到的結果,因為它在一個 foreach 循環中,是多個所見即所得的編輯器字段。

我嘗試了很多東西,這是其中之一的示例代碼:('cond_options' - 多選'description' - 所見即所得編輯器''天氣/天空/夜晚' - 過濾值)

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;

在不了解您的數據和您試圖實現的實際最終結果的情況下。 我整理了一些可能有助於引導您朝着正確方向前進的東西。

主要要指出的是使用in_array ,這樣我們可以檢查多個 select 中是否存在一個值,而無需使用 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!
                }
            }
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM