如何添加选项以从 php 数组中选择字段到联系表单 7 Wordpress


How to add options to select field from php array to Contact form 7 Wordpress

我在wordpress中有一个单页网站。我需要将来自 php 数组 $cat_good 的信息放在两个选择框中。它在索引.php文件中工作正常,如下所示:

<div>
    <select id="flowers_type" class="styled">
        <option value="">--</option>
        <?php 
            foreach ( $cat_good as $key => $value) {
                echo '<option value="' . $key . '">' . $key . '</option>';
            } 
        ?>
    </select>
</div>
<div>
    <select id="flowers_type_item" class="styled">
        <option value="">--</option>
        <?php 
            foreach ( $cat_good as $key => $value) {
                $good = $key;
                foreach ( $value as $key => $value ) {
                    echo '<option class="' . $good . '" value="' . $key . '">' . $value . '</option>'; 
                }
            } 
        ?> 
    </select>
</div>

问题是,如何将这两个选择框放入联系表格7?

在 Dhanuka Nuwan 的帮助下,现在我有了函数中的代码.php,这有助于我将选择器添加到联系表单 7。

function flowers_type(){<!-- here is my code for $cat_good -->
$output .= "<div><select name='flowers_type' id='flowers_1' class='styled'><option value='0'>--</option>";      
    foreach ( $cat_good as $key => $value) {
        $output .= "<option value='$key'>$key</option>";         
     }
$output .= "</select></div>";
$output .= "<div><select name='flowers_type_item' id='flowers_2' class='styled'><option value='0'>--</option>";     
    foreach ( $cat_good as $key => $value) {
        $good = $key;
        foreach ( $value as $key => $value ) {
            $output .= "<option class='$good' value='$key'>$value</option>";
        }
     }
     $output .= "</select></div>";
     return $output;}

但我也需要从第一个选择器依赖第二个选择器。我正在尝试在 https://github.com/tuupola/jquery_chained 的帮助下做到这一点。在我的js文件中,我有:

$("#flowers_2").chained("#flowers_1");

不幸的是,它不起作用。

您可以使用wpcf7_add_shortcode轻松地将短代码添加到联系表格 7。这是您的代码。

function flowers_type(){
   $output = "<select name='flowers_type' id='flowers_type' onchange='document.getElementById('"flowers_type'").value=this.value;'><option value="">--</option>";
   foreach ( $cat_good as $key => $value) {
            $output .= "<option value='$key'> $key </option>";
        } 
   $output .= "</select>";
return $output;
}
wpcf7_add_shortcode('flowers_type', 'flowers_type', true);

现在,您可以在联系表格 7 表格中使用 [flowers_type] 简码。请注意,此代码未经测试。使用前备份文件。对你的另一个做同样的事情。

玩得愉快。 :)