2016-08-21 126 views
0

我正在使用重力形式插件,并试图在我已创建的窗体中显示类别作为下拉列表。从数据库中提取数据并以重力形式显示

enter image description here

如果需要的话,请在这里对my website

我已经在这个时间过长的链接,并没有出路。请帮助我。

add_filter('gform_pre_render_1', 'populate_categories'); 
 
add_filter('gform_pre_validation_1', 'populate_categories'); 
 
add_filter('gform_pre_submission_filter_1', 'populate_categories'); 
 
add_filter('gform_admin_pre_render_1', 'populate_categories'); 
 
function populate_categories($form) { 
 

 
    foreach ($form['fields'] as &$field) { 
 

 
     if ($field->id != 1) { 
 
      continue; 
 
     } 
 

 
     // you can add additional parameters here to alter the posts that are retrieved 
 
     // more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts) 
 
     $categories = get_categories ; 
 

 
     $choices = array(); 
 

 
     foreach ($categories as $categories) { 
 
      $choices[] = array('text' => $categories->name, 'value' => $categories->name); 
 
     } 
 

 
     // update 'Select a Post' to whatever you'd like the instructive option to be 
 
     $field->placeholder = 'Category'; 
 
     $field->choices = $choices; 
 

 
    } 
 

 
    return $form; 
 
}

回答

0

可以动态生成使用这个链接下面提供的语法形式重心的下拉菜单。您必须控制主题的functions.php文件以按照您的要求检索您的输出。

以下是提供的清晰文档,您可以使用此方法以简单方式创建。有两种方法可以参考它。

https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/

如果你面对任何创作问题让我知道,我们将解决这个问题。

+0

嗨库马尔,谢谢你的帮助,对不起,这个回复迟到了,我做了一个紧急的行程,让项目暂停。我按照你发送的链接上的步骤进行了操作,但它没有给我列出我的类别,也许我没有把它做对。我已经添加了我在这篇文章中使用的php代码,以防万一你想看看它。再次感谢 –

相关问题