2015-02-05 37 views
0

有沒有辦法根據內容類型創建/更新表單輪詢字段?例如,我有一個名爲Candidate的帖子類型,我希望在添加新候選人或刪除某個候選人時動態更新投票列表。創建基於帖子類型的重力形式輪詢

我尋找這個的原因是因爲我爲這個客戶創建了一個投票機制,他們要求用戶看到他們投票的圖像,名稱和簡要簡歷。我的想法是綁定名稱,以便我可以在頁面上定位隱藏的Gravity Form輪詢,因此當選舉人單擊時,它會更新相應的指定複選框。

我當然可以逐個添加每個候選人,然後在表單中逐個添加每個候選人,但希望有一種方法可以在代碼中執行此操作。到目前爲止,我還沒有在Gravity Forms Documentation中找到很多其他的過濾器。

重申,我的問題不是前端連接,而是在爲內容創建Candidate發佈類型時如何在表單中的輪詢字段中動態更新/添加選項。

任何幫助,將不勝感激。

回答

1

我相信你在這裏是後是該解決方案:

http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_pre_render/

可能的解決方案的例子#1頁上的組合和#2將滿足您的需求?所以 -

  1. 創建標準的WordPress網頁類別(類別名稱:「考生」),並在頁面將包含考生信息(生物,照片等)。

  2. 在您當前主題的functions.php文件,你會加入類似下面拉出該類別的價值帖子:

    // modify form output for the public page 
    add_filter("gform_pre_render", "populate_checkbox"); 
    // modify form in admin 
    add_filter("gform_admin_pre_render", "populate_checkbox"); 
    
    function populate_dropdown($form) { 
        // some kind of logic/code to limit what form you're editing 
        if ($form["id"] != 1) { return $form; } 
    
        //Reading posts for "Business" category; 
        $posts = get_posts("category=" . get_cat_ID("Candidates")); 
    
        foreach ($form['fields'] as &$field) { 
    
         // assuming field #1, if this is a voting form that uses checkboxes 
         $target_field = 1; 
         if ($field['id'] != $target_field) { break; } 
    
         // init the counting var for how many checkboxes we'll be outputting 
         // there's some kind of error with checkboxes and multiples of 10? 
         $input_id = 1; 
         foreach ($posts as $post) { 
    
          //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs) 
          if($input_id % 10 == 0) { $input_id++; } 
    
          // here's where you format your field inputs as you need 
          $choices[] = array('text' => $post->post_title, 'value' => $post->post_title); 
          $inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}"); 
    
          // increment the number of checkboxes for ID handling 
          $input_id++; 
         } 
    
         $field['choices'] = $choices; 
         $field['inputs'] = $inputs; 
        } 
    
        // return the updated form 
        return $form; 
    }