Advertisement
chrishajer

Populate drop down with categories

Sep 2nd, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/populate-a-drop-with-categories-from-current-post
  2. // update the '33' to the ID of your form
  3. add_filter('gform_pre_render_33', 'populate_cats');
  4. function populate_cats($form){
  5.         // loop through each of the form fields...
  6.         foreach($form['fields'] as &$field){
  7.  
  8.                 // ... and skip this function if this is not a Select box or a cssClass of populate-categories is not present
  9.                 // be sure to add the CSS Class to your select box Advanced tab
  10.                 if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-categories') === false)
  11.                         continue;
  12.  
  13.                 // here's where we populate the drop down
  14.                 // update 'Rim Size' to whatever you'd like the instructive option to be
  15.                 $choices = array(array('text' => 'Rim Size', 'value' => ' '));
  16.  
  17.                 // the WordPress function get_the_category will return all categories for the current post
  18.                 // http://codex.wordpress.org/Function_Reference/get_the_category
  19.                 foreach((get_the_category()) as $category) {
  20.                         $choices[] = array('text' => $category->cat_name, 'value' => $category->cat_name);
  21.                 }
  22.         // populate $field['choices'] with our array of choices
  23.         $field['choices'] = $choices;
  24.         }
  25.         // always return the $form
  26.         return $form;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement