【问题标题】:selected not saved ?simple bug ?/ I am beginner选择未保存?简单的错误?/我是初学者
【发布时间】:2013-10-18 08:53:06
【问题描述】:

我有这个功能,它给了我下拉选项:

public function edit_tax_image_field( $term ){
    $term_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$term_id" );
    $image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
?>
    <tr class="form-field">
        <th scope="row">
            <label for="term_meta[tax_image]">Dropdown menu</label>
            <td>
                <select name="term_meta[tax_image]" id="term_meta[tax_image]" style="width: 300px;">
    <?php
        $selected = $image;
        $p = '';
        $r = '';

        foreach ( _s_sample_select_options() as $option ) {
            $label = $option['label'];
            if ( $selected == $option['value'] ) // Make default first in list
                $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
            else
                $r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>";
        }
        echo $p . $r;
    ?></select>
                <p class="description">Some description.</p>
            </td>
        </th>
    </tr><!-- /.form-field -->
<?php
} // edit_tax_image_field

这是我的保存功能

public function save_tax_meta( $term_id ){

    if ( isset( $_POST['term_meta'] ) ) {

        $t_id = $term_id;
        $term_meta = array();

        $term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_attr( $_POST['term_meta']['tax_image'] ) : '';

        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );

    } // if isset( $_POST['term_meta'] )
} // save_tax_meta

我不知道,应该是哪里的错误。我是编码初学者,所以......错误可能很简单。

解决这个问题花了我一天的时间 :D 仍然没有解决方案 :(

【问题讨论】:

  • 你在哪里调用这些函数?你确定 $term 没有返回 null 吗?
  • 在主文件(index.php)中,我正在加载所有必要的操作:add_action( 'product_cat_add_form_fields', array( $this, 'add_tax_image_field' ) );add_action( 'product_cat_edit_form_fields', array( $this, 'edit_tax_image_field' ) );// savingadd_action( 'product_cat_edit_form_fields', array( $this, 'save_tax_meta' ), 10, 2 );add_action( 'product_cat_add_form_fields', array( $this, 'save_tax_meta' ), 10, 2 ); Categories.php 是脚本运行的地方。
  • 我尝试了另一个脚本,但仍然没有运气 :( 似乎没有新元
  • ttry使用exit逐步调试;查看代码到底在哪里中断
  • 调试什么也没给我。但我想,selected 不起作用:(其余代码还可以

标签: wordpress select save option


【解决方案1】:

尝试改变

if ( $selected == $option['value'] ) // Make default first in list
                $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";

if ( $selected == $option['value'] ) // Make default first in list
                $p .= "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";

【讨论】:

  • 感谢您的尝试,但问题出在 $selected = $image; 我尝试了:$selected = $_POST['term_meta']['tax_image'];$selected = $_POST['term_meta']$selected = $options['term_meta'] 没有任何帮助 :(
  • 你明白了:D 我做了你在此处发布的更改,而不是 $selected = $image();
猜你喜欢
  • 2021-12-05
  • 2016-11-17
  • 2013-03-14
  • 2013-09-13
  • 2021-09-02
  • 2020-07-13
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
相关资源
最近更新 更多