programing

워드프레스 REST API : WP REST API JSON 파일에서 "단어 전용" 콘텐츠를 얻는 방법은?

subpage 2023. 10. 31. 22:08
반응형

워드프레스 REST API : WP REST API JSON 파일에서 "단어 전용" 콘텐츠를 얻는 방법은?

WP REST API를 사용하여 웹 사이트에서 데이터를 검색하고 있습니다. 예를 들어 http://localhost:88888/wordpress/wp-json/wp/v2/posts/42 게시물의 정보를 얻을 수 있습니다. 게시물 42의 정보는 알 수 있지만 내용 섹션 안에는 다음과 같이 표시됩니다.

enter image description here

실제 게시물은 다음과 같은 형식입니다.

테스트블로그 입니다 +[이미지] +테스트블로그 입니다 +[이미지]

제가 컨텐츠 부분에서 원하는 것은 이미지의 정보가 아닌 단어 뿐인데, 이를 달성하기 위해 제가 할 수 있는 일은 무엇인가요?

그리고 WP REST API가 이 컨텐츠 섹션에 대해 어떤 형식으로 반환되었습니까?웹사이트에서 읽어보니 '물건'이라고 적혀있더군요.저는 WP가 처음입니다.

당신은 당신이 할 수 있는rest_api_init필요한 내용을 수정할 수 있습니다.

add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'post',
          'content',
          array(
                 'get_callback'    => 'do_raw_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function do_raw_shortcodes( $object, $field_name, $request )
{
   global $post;
   $post = get_post ($object['id']);
   // This is what we currently have...
   $output['rendered'] = apply_filters( 'the_content',  $post->post_content);
   // This includes the shortcodes
   $output['_raw'] = $post->post_content;
   // Add something custom
   $output['foo'] = 'bar';
   return $output;
}

그러면 JSON에 있는 여분의 데이터를 봐야 합니다.

언급URL : https://stackoverflow.com/questions/39097701/wordpress-rest-api-how-to-get-the-word-only-content-in-wp-rest-api-json-file

반응형