So here is how this will have to be done. It's a little bit of a work around.
What you will have to do is add a TEXT FIELD to your form and name it what you want the post title drop down to be. What we need to do is use PHP code to change the display of a TEXT FIELD into a drop down. So instead of adding a drop down to your form, add a text field. The information will be stored in this text field.
Here is the code snippet that will transform this text field into a drop down that consists of post titles for a given category id...
Note, you will need to change the FORM ID, the CATEGORY, and the FIELD ID in this code snippet.
//Adds a filter to form id 14. Replace 14 with your actual form id
add_filter("gform_pre_render_14", populate_dropdown);
function populate_dropdown($form){
//Reading posts for "Business" category;
$posts = get_posts("category=Business");
//Creating drop down item array.
$items = array();
//Adding initial blank value.
$items[] = array("text" => "", "value" => "");
//Adding post titles to the items array
foreach($posts as $post)
$items[] = array("value" => $post->post_title, "text" => $post->post_title);
//Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 8){
$field["type"] = "select";
$field["choices"] = $items;
}
return $form;
}
The form id you need to change is the 14 in this piece of the code snippet above:
add_filter("gform_pre_render_14", populate_dropdown);
The category you need to change is the name in this piece of the code snippet above:
$posts = get_posts("category=Business");
The field id you need to change is the id in this piece of the code snippet above:
if($field["id"] == 8){
You can get the field id by viewing source on the form front end, finding the field we are targeting and getting the id.
Posted 14 years ago on Tuesday January 12, 2010 |
Permalink