如何将 select 字段的选项从 php 数组添加到联系表 7 Wordpress

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

我在 wordpress 中有一个单页网站。我需要将来自 php 数组 $cat_good 的信息放入两个 select 框中。它在 index.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>

问题是,如何将这两个select框放入contact form 7?

在 Dhanuka Nuwan 的帮助下,我现在有了 function.php 中的代码,这有助于我将 select 或添加到联系表 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;}

但我还需要第二个 selector 依赖第一个。我正在尝试在 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);

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

玩得开心。 :)