admin管理员组

文章数量:1277888

/**
 * Add a Formatted Date to the WordPress REST API JSON Post Object
 *
 */
add_action('rest_api_init', function() {
   register_rest_field(
      array('comment'),
      'formatted_date',
      array(
         'get_callback' => function() {
            return get_the_date();
         },
         'update_callback' => null,
         'schema' => null,
      )
   );
});

This is not working for me. I am constantly getting the following in my JSON:

"formatted_date":false,"

How can I show it in a format like: "28 November 2021, 15:00"?

/**
 * Add a Formatted Date to the WordPress REST API JSON Post Object
 *
 */
add_action('rest_api_init', function() {
   register_rest_field(
      array('comment'),
      'formatted_date',
      array(
         'get_callback' => function() {
            return get_the_date();
         },
         'update_callback' => null,
         'schema' => null,
      )
   );
});

This is not working for me. I am constantly getting the following in my JSON:

"formatted_date":false,"

How can I show it in a format like: "28 November 2021, 15:00"?

Share Improve this question asked Nov 17, 2021 at 15:24 JohanJohan 3091 gold badge10 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

In general I would be careful modifying existing endpoints, but you could try using the object passed into the callback, e.g.:

'get_callback' => function( $object ) {
    return date_i18n( __( 'j. F Y, H:i', 'wpse' ), strtotime( $object['date'] ) );
 },

本文标签: How to change the date and time in REST API for comments