我试图在重力表单中预填充一个类别字段或发布下拉列表。以下是我所拥有的:
<?php
add_filter("gform_pre_render", "gform_prepopluate_populate_books");
//Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_books");
function gform_prepopluate_populate_books($form){
$posttype = \'books\';
$taxtype = \'genres\';
$formid = 5;
$fieldid = 14);
//only populating drop down for form id 5
if($form["id"] != $formid)
return $form;
//Reading posts for "Business" category;
//$posts = query_posts(array(\'post_type\' => array(\'post\', \'movies\')));
// $posts = query_posts( array( \'post_type\' => $querytype ) );
$taxonomies=get_taxonomies(array(\'name\' => $taxtype ), \'names\');
// if ( have_posts() ) : while ( have_posts() ) : the_post();
//Creating drop down item array.
$items = array();
//Adding initial blank value.
$items[] = array("text" => "", "value" => "");
//Adding post titles to the items array
if ( $taxonomies ) {
foreach($taxonomies as $taxonomy) {
$items[] = array("value" => $taxonomy->slug, "text" => $taxonomy->name);
}
}
//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"] == $fieldid ) {
$field["choices"] = $items;
}
}
似乎无法将分类法值填充到下拉列表中。有什么想法吗?