亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重力形式:動態創建字段集

重力形式:動態創建字段集

PHP
尚方寶劍之說 2023-04-21 15:04:59
我想讓用戶從下拉字段中選擇自定義帖子類型并向其添加一些額外數據。例如:用戶可以從列表中選擇一部電影。對于那部電影,他可以添加一定數量的副本,直到他想借到為止。所以我一共有三個字段:帶有電影的下拉字段數字字段日期字段我現在想為 WordPress 中的每部電影添加此字段集(自定義帖子類型)。因為我不知道我們在 WordPress 中有多少電影,所以我想動態生成字段。幸運的是,我從 Gravity Forms 找到了 Repeater (beta) 字段。使用該字段,用戶可以根據需要添加/刪除電影。演示和文檔: https: //docs.gravityforms.com/repeater-fields/問題是,我需要用 WordPress 中的電影 CPT 填充第一個字段(下拉列表)。這是我當前的代碼,用于生成表單中的轉發器字段:// Adjust your form IDadd_filter( 'gform_form_post_get_meta_5', 'add_my_field' );function add_my_field( $form ) {    $movie_sku = GF_Fields::create( array(        'type'   => 'text',        'id'     => 1002, // The Field ID must be unique on the form        'formId' => $form['id'],        'required' => true,        'label'  => 'Movie',        'class'  => 'col-md-4',        'pageNumber'  => 1, // Ensure this is correct    ) );    $movie_amount = GF_Fields::create( array(        'type'   => 'text',        'id'     => 1007, // The Field ID must be unique on the form        'formId' => $form['id'],        'required' => true,        'label'  => 'Amount',        'pageNumber'  => 1, // Ensure this is correct    ) );    $movie_date = GF_Fields::create( array(        'type'   => 'text',        'id'     => 1001, // The Field ID must be unique on the form        'formId' => $form['id'],        'required' => true,        'label'  => 'Date',        'pageNumber'  => 1, // Ensure this is correct    ) );    $movie = GF_Fields::create( array(        'type'             => 'repeater',        'required'          => true,        'id'               => 1000, // The Field ID must be unique on the form        'formId'           => $form['id'],        'label'            => 'Add movie',        'addButtonText'    => 'Add Another movie',        'removeButtonText'=> 'Remove movie',        'pageNumber'       => 1, // Ensure this is correct        'fields'           => array( $movie_sku,$movie_amount, $movie_date), // Add the fields here.    ) );    $form['fields'][] = $movie;    return $form;}
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

我想我找到了解決方案:


在第一個之前,GF_Fields::create我必須從第二個函數中復制以下代碼:


$args = array(

        'orderby'       =>  'title',

        'order'         =>  'ASC',

        'numberposts'   => -1,

        'post_type'     => 'movie',

        'post_status'   => array('publish'),


        );


        $posts = get_posts( $args );


        $choices = array();


        foreach ( $posts as $post ) {


            $choices[] = array(

                'text'  => $post->post_title,

                'value' => $post->post_title

            );

        }

然后我必須GF_Fields::create像這樣編輯第一個:


$movie_sku = GF_Fields::create( array(

    'type'   => 'select',

    'id'     => 1002, // The Field ID must be unique on the form

    'formId' => $form['id'],

    'required' => true,

    'label'  => 'Movie',

    'choices'  => $choices,

    'pageNumber'  => 1, // Ensure this is correct

) );

新部分是'choices'  => $choices,從上面的代碼中獲取數據的部分。當然,我必須將輸入類型更改為 select: 'type'   => 'select',。


查看完整回答
反對 回復 2023-04-21
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號