我能够将“自定义字段”添加到“自定义分类法”中,并使用以下代码进行保存。我检查了数据库,结果保存到wp_options
表中,如
当我尝试获得分类术语时,我只会得到诸如name,slug和...等内置值。
array(2){[0] => object(WP_Term)#4243(10){[“ term_id”] => int(7)[“ name”] => string(2)“ BB” [“ slug“] =>字符串(2)” bb“ [” term_group“] =>整数(0)[” term_taxonomy_id“] =>整数(7)[”分类法“] =>字符串(6)”类型“ [” description“] => string(0)”“ [”“” parent“] => int(0)[” count“] => int(0)[” filter“] => string(3)” raw“} {{ 3}} => object(WP_Term)#4242(10){[“ term_id”] => int(6)[“ name”] =>字符串(2)“ ZZ” [“ slug”] =>字符串(2 )“ zz” [“ term_group”] => int(0)[“ term_taxonomy_id”] => int(6)[“ taxonomy”] =>字符串(6)“类型” [“描述”] =>字符串(0 )“” [“”父母“] => int(0)[” count“] => int(0)[” filter“] => string(3)” raw“}}
但没有自定义字段值!如何访问它并将其显示在页面上?
function mj_taxonomy_edit_custom_meta_field($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[class_term_meta]"><?php _e( 'Dad Name','MJ' ); ?></label></th>
<td>
<input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value="<?php echo esc_attr( $term_meta['class_term_meta'] ) ? esc_attr( $term_meta['class_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'genres_add_form_fields','mj_taxonomy_edit_custom_meta_field',10,2 );
add_action( 'genres_edit_form_fields',2 );
function mj_save_taxonomy_custom_meta_field( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id",$term_meta );
}
}
add_action( 'edited_genres','mj_save_taxonomy_custom_meta_field',2 );
add_action( 'create_genres',2 );