Gravityforms - 添加类别 mid-radio-button 字段
Gravityforms - Add category mid-radio-button field
我正在使用我的模板中的 functions.php 填充 Gravityforms 字段,它就像一个魅力,但我有一个字段是......一个挑战。我已经能够很好地从我的数据库中填充选项,但是使用 functions.php 我无法控制字段显示区域的内容,例如,我可以添加标题或 header 对于每个类别。有没有办法以编程方式调整显示这是我希望完成的示例
RadioButton Choice (Field ID 21)
Dark Colors (category title)
maroon (choice 1)
navy blue (choice 2)
black (Choice 3)
Standard Colors (Category Title)
Red (choice 4)
blue (choice 5)
gray (Choice 6)
Light Colors )Category Title)
pink (choice 7)
sky blue (choice 8)
white (Choice 9)
我正在寻找一种在选项之间添加类别标题的方法。我的数据库查询将类别作为响应的一部分,但我必须填充选择以提供数组的唯一选项。
我已经看到我可以在哪里添加额外的 Gravityform 字段并让它们由相同的“单个 select”单选按钮选项控制,但是涉及的类别会根据我调用的数据库查询而变化以动态填充选择范围从 1 个类别到 10 个类别,它们在表单本身中没有相关字段,因为它们都在一个 radio-button 字段下。
如有任何想法,我们将不胜感激。
我能够使用我在评论中发布的 link 来提供解决方案。作为我“选择”的一部分
这是预渲染填充选项的功能。在这种情况下,我用产品图片而不是默认单选按钮填充单选按钮字段
add_filter('gform_pre_render_1', 'populate_choices');
function populate_choices($form) {
global $wpdb;
// Now to get a list of the products I want to include on this radio-button
$dataQ = "
SELECT category, product_id, product_name
FROM product_table
WHERE product_type = 'whatever I am looking for'
ORDER BY category, product_name ASC
";
$dataR = $wpdb->get_results($dataQ);
$dataList = array();
// get current protocol to use in Product Images
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// Rebuild URL for use with product image graphics
$productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';
// Generate list of choices for the radio-button, along with category headers
foreach ($form['fields'] as $field) {
if ($field->id == '32') { // 32 My specific radio-button field
$category = "";
foreach ($dataR as $data) {
if ($category != $data->category) {
// If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
$category = $data->category;
$dataList[] = array(
'text' => '#'.$category."#",
'value' => ''
);
}
$productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
$productID = $data->product_id;
$dataList[] = array(
'text' => $data->product_name,
'value' => $productID,
'isSelected' => false,
'price' => '0.00',
'imageChoices_image' => $productImagehUrl.$productImage
);
}
$field->choices = $dataList;
}
}
}
然后我添加了一个特定的字段修饰符以使用 html 更新“#category#”选择元素,使类别名称显示出来。
// numbers after filter = "1" for form ID 1, "32" for field ID 32
add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
if ( strpos( $choice['text'], '#' ) === 0 ) {
$categoryName = str_replace("#","",$choice['text']);
$choice_markup = '
<li style="width:100%;text-align:left;">
<span style="color:#000000;font-weight:bold;font-size:18px;">
<br>
'.$categoryName.'
</span>
</li>
';
}
return $choice_markup;
}, 10, 2);
我正在使用我的模板中的 functions.php 填充 Gravityforms 字段,它就像一个魅力,但我有一个字段是......一个挑战。我已经能够很好地从我的数据库中填充选项,但是使用 functions.php 我无法控制字段显示区域的内容,例如,我可以添加标题或 header 对于每个类别。有没有办法以编程方式调整显示这是我希望完成的示例
RadioButton Choice (Field ID 21)
Dark Colors (category title)
maroon (choice 1)
navy blue (choice 2)
black (Choice 3)
Standard Colors (Category Title)
Red (choice 4)
blue (choice 5)
gray (Choice 6)
Light Colors )Category Title)
pink (choice 7)
sky blue (choice 8)
white (Choice 9)
我正在寻找一种在选项之间添加类别标题的方法。我的数据库查询将类别作为响应的一部分,但我必须填充选择以提供数组的唯一选项。
我已经看到我可以在哪里添加额外的 Gravityform 字段并让它们由相同的“单个 select”单选按钮选项控制,但是涉及的类别会根据我调用的数据库查询而变化以动态填充选择范围从 1 个类别到 10 个类别,它们在表单本身中没有相关字段,因为它们都在一个 radio-button 字段下。
如有任何想法,我们将不胜感激。
我能够使用我在评论中发布的 link 来提供解决方案。作为我“选择”的一部分
这是预渲染填充选项的功能。在这种情况下,我用产品图片而不是默认单选按钮填充单选按钮字段
add_filter('gform_pre_render_1', 'populate_choices');
function populate_choices($form) {
global $wpdb;
// Now to get a list of the products I want to include on this radio-button
$dataQ = "
SELECT category, product_id, product_name
FROM product_table
WHERE product_type = 'whatever I am looking for'
ORDER BY category, product_name ASC
";
$dataR = $wpdb->get_results($dataQ);
$dataList = array();
// get current protocol to use in Product Images
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// Rebuild URL for use with product image graphics
$productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';
// Generate list of choices for the radio-button, along with category headers
foreach ($form['fields'] as $field) {
if ($field->id == '32') { // 32 My specific radio-button field
$category = "";
foreach ($dataR as $data) {
if ($category != $data->category) {
// If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
$category = $data->category;
$dataList[] = array(
'text' => '#'.$category."#",
'value' => ''
);
}
$productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
$productID = $data->product_id;
$dataList[] = array(
'text' => $data->product_name,
'value' => $productID,
'isSelected' => false,
'price' => '0.00',
'imageChoices_image' => $productImagehUrl.$productImage
);
}
$field->choices = $dataList;
}
}
}
然后我添加了一个特定的字段修饰符以使用 html 更新“#category#”选择元素,使类别名称显示出来。
// numbers after filter = "1" for form ID 1, "32" for field ID 32
add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
if ( strpos( $choice['text'], '#' ) === 0 ) {
$categoryName = str_replace("#","",$choice['text']);
$choice_markup = '
<li style="width:100%;text-align:left;">
<span style="color:#000000;font-weight:bold;font-size:18px;">
<br>
'.$categoryName.'
</span>
</li>
';
}
return $choice_markup;
}, 10, 2);