如何从WP rest API中检索类别分类的自定义元术语?

时间:2020-09-28 作者:dardar.moh

首先,我添加了名为Color 有关类别分类,请参见下面的代码

Add new colorpicker field to "Add new Category" screen

function colorpicker_field_add_new_category( $taxonomy ) {

  ?>

    <div class="form-field term-colorpicker-wrap">
        <label for="term-colorpicker">Category Color</label>
        <input name="_category_color" value="#ffffff" class="colorpicker" id="term-colorpicker" />
        <p>This is the field description where you can tell the user how the color is used in the theme.</p>
    </div>

  <?php

}
add_action( \'category_add_form_fields\', \'colorpicker_field_add_new_category\' );
function colorpicker_field_add_new_category( $taxonomy ) {

  ?>

    <div class="form-field term-colorpicker-wrap">
        <label for="term-colorpicker">Category Color</label>
        <input name="_category_color" value="#ffffff" class="colorpicker" id="term-colorpicker" />
        <p>This is the field description where you can tell the user how the color is used in the theme.</p>
    </div>

  <?php

}
add_action( \'category_add_form_fields\', \'colorpicker_field_add_new_category\' );

Term Metadata - Save Created and Edited Term Metadata

function save_termmeta( $term_id ) {

    // Save term color if possible
    if( isset( $_POST[\'_category_color\'] ) && ! empty( $_POST[\'_category_color\'] ) ) {
        update_term_meta( $term_id, \'_category_color\', sanitize_hex_color_no_hash( $_POST[\'_category_color\'] ) );
    } else {
        delete_term_meta( $term_id, \'_category_color\' );
    }

}
add_action( \'created_category\', \'save_termmeta\' );  // Variable Hook Name
add_action( \'edited_category\',  \'save_termmeta\' );  // Variable Hook Name

Enqueue colorpicker styles and scripts.

function category_colorpicker_enqueue( $taxonomy ) {

    if( null !== ( $screen = get_current_screen() ) && \'edit-category\' !== $screen->id ) {
        return;
    }

    // Colorpicker Scripts
    wp_enqueue_script( \'wp-color-picker\' );

    // Colorpicker Styles
    wp_enqueue_style( \'wp-color-picker\' );

}
add_action( \'admin_enqueue_scripts\', \'category_colorpicker_enqueue\' );

Print javascript to initialize the colorpicker

function colorpicker_init_inline() {
    if( null !== ( $screen = get_current_screen() ) && \'edit-category\' !== $screen->id ) {
        return;
    }
  ?>
    <script>
        jQuery( document ).ready( function( $ ) {
            $( \'.colorpicker\' ).wpColorPicker();
        } ); // End Document Ready JQuery
    </script>
  <?php
}
add_action( \'admin_print_scripts\', \'colorpicker_init_inline\', 20 );
一切都很好,结果如下

enter image description here

现在,当我访问http://localhost/wp/wp-json/wp/v2/posts?_embed 我看不到列出的新元术语,请参见下图:

enter image description here

那么,我的问题是如何检索新的元术语?我错过什么了吗?

1 个回复
最合适的回答,由SO网友:Paul 整理而成

首先,您需要告诉WordPress在API响应中显示此自定义字段:register_term_meta(\'category\', \'_category_color\', [\'show_in_rest\' => true]);

然后,您可以过滤类别术语API端点,如下所示:

add_filter( \'rest_prepare_category\',function($response, $item, $request){
    $color = get_term_meta( $item->term_id, \'_category_color\', true );
    $response->data[\'color\'] = $color ?: \'\';
    return $response;
}, 10, 3);