如何从wordpress自定义分类字段回显日期

时间:2016-04-25 20:08:10

标签: wordpress

我的分类表格中有4个defualt字段(Name,slug,parent,description)。但我想添加新字段,具体 - >日期。所以我把这段代码放到我主题的functions.php中。

function my_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'field' ); ?></label>
        <input type="date" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
        <p class="description"><?php _e( 'Enter a value for this field','field' ); ?></p>
    </div>
<?php
}
add_action( 'wp-type-activity-types_add_form_fields', 'my_taxonomy_add_new_meta_field', 10, 2 );

function my_taxonomy_edit_meta_field($term) {

    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'field' ); ?></label></th>
        <td>
            <input type="date" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter a value for this field','field' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'wp-type-activity-types_edit_form_fields', 'my_taxonomy_edit_meta_field', 10, 2 );

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $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_wp-type-activity-types', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_wp-type-activity-types', 'save_taxonomy_custom_meta', 10, 2 );

现在我被卡住了,因为不知道如何回应我的约会。

我只能回复4个defualt字段(Name,slug,parent,description)。

$my_desc = get_term_field( 'description', $value->term_id);
echo $my_desc;

那么为什么我不能回应我的自定义字段 - &gt;约会,就像那样:

$my_date = get_term_field( 'term_meta[custom_term_meta]', $value->term_id);
echo $my_date;

0 个答案:

没有答案