我陷入了为wordpress类别创建选择自定义字段并在管理表中显示所选元数据的问题。
我有一个代码可以创建类别中的自定义字段,并将其保存在数据库中。它还在管理表中创建列。我的问题是,它始终显示第一个值,而不显示所选的值。我做了一个if语句,但是没有用。你能告诉我我哪里做错了吗?
代码:
<?php
function add_extra_fields_to_category($taxonomy_name){
?>
<div class="form-field">
<label for="category-realized">Was the project finished?</label>
<select name="category-realized" id="category-realized">
<option value="2"> No </option>
<option value="1"> Yes </option>
</select>
</div>
<?php
}
add_action('category_add_form_fields','add_extra_fields_to_category');
function save_extra_taxonomy_fields($term_id){
//collect all term related data for this new taxonomy
$term_item = get_term($term_id);
//collect our custom fields
$term_category_select = sanitize_text_field($_POST['category-realized']);
//save our custom fields as wp-options
update_term_meta($term_id, 'term_category_realized', $term_category_select);
}
add_action('create_category','save_extra_taxonomy_fields');
function edit_extra_fields_for_category($term){
//collect our saved term field information
$term_category_select = get_term_meta('term_category_realized');
//output our additional fields?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="category-realized">Was the project finished?</label>
</th>
<td>
<select name="category-realized" id="category-realized" value="<?php echo $term_category_select; ?>">
<option value="2" <?php if($term_category_select === '2'){ echo 'selected';}?>> No </option>
<option value="1" <?php if($term_category_select === '1'){ echo 'selected';}?>> Yes </option>
</select>
</td>
</tr>
<?php
}
add_action('category_edit_form_fields','edit_extra_fields_for_category');
add_action('edit_category','save_extra_taxonomy_fields');
// Add column to Category list
function category_realized_columns($columns)
{
return array_merge($columns, array('term_category_realized' => __('Was the project finished?')));
}
add_filter('manage_edit-category_columns' , 'category_realized_columns');
// Add the value to the column
function category_realized_columns_values( $deprecated, $column_name, $term_id) {
if($column_name === 'term_category_realized'){
$meta = get_term_meta($term_id);
echo '<pre>'; var_export($meta['term_category_realized']); echo '</pre>';
if ($meta['term_category_realized'] === '1') {
echo _e('Yes');
} else {
echo _e('No');
}
}
}
add_action( 'manage_category_custom_column' , 'category_realized_columns_values', 10, 3 );
?>