【发布时间】: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