WordPress REST API에서 원시 HTML 출력 가져오기
WordPress REST API를 사용하여 외부 응용 프로그램에서 WordPress 페이지의 HTML 콘텐츠를 가져옵니다.이 사이트를 mysite/wp-json/wp/v2/pages/10이라고 부르면 다음과 같이 반환됩니다.
"content": {
"rendered": "[vc_column_text]Hello World[/vc_column_text]"
}
[vc_] 쇼트 코드 없이 최종 HTML 출력으로 코드를 반환할 수 있는 방법이 있습니까?<p>Hello World</p>
단축 코드는 Visual Composer 페이지 작성기 플러그인에서 가져옵니다.
여기에서 검색하여 답변: https://github.com/CompassHB/web/issues/67#issuecomment-245857301
다음 예는 위의 링크에서 가져온 것입니다.
/**
* Modify REST API content for pages to force
* shortcodes to render since Visual Composer does not
* do this
*/
add_action( 'rest_api_init', function ()
{
register_rest_field(
'page',
'content',
array(
'get_callback' => 'compasshb_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function compasshb_do_shortcodes( $object, $field_name, $request )
{
WPBMap::addAllMappedShortcodes(); // This does all the work
global $post;
$post = get_post ($object['id']);
$output['rendered'] = apply_filters( 'the_content', $post->post_content );
// EDIT: add custom CSS to $output:
$output[ 'yb_wpb_post_custom_css' ] = get_post_meta( $object[ 'id' ], '_wpb_post_custom_css', true);
return $output;
}
편집
코멘트에서 질문이 제기되었습니다.커스텀 CSS 세트를 페이지(게시물 등)에 입수하는 방법REST API 응답에 커스텀 CSS가 추가되도록 샘플 코드를 수정하였습니다.CSS는 다음 URL에 있습니다.content/yb_wpb_post_custom_css
.
다른 방법은 이 CSS를 포함하는 REST API 응답에 다른 필드를 추가하는 것입니다.중요한 것은 커스텀 CSS가 페이지/포스트 등에 설정되어 있는 것입니다.에는 메타키 _wpb_post_custom_css가 있습니다.
하지만 파티에 2년 정도 늦게 도착했는데, 다음과 같은 일이 있었습니다.
$output['rendered'] = apply_filters( 'the_content', get_the_content() );
혹시 궁금하실까 봐.
언급URL : https://stackoverflow.com/questions/35432185/get-raw-html-output-from-wordpress-rest-api
'sourcecode' 카테고리의 다른 글
AJAX(특히 jQuery)를 통해 로드된 Javascript를 디버깅하려면 어떻게 해야 합니까? (0) | 2023.02.11 |
---|---|
woocommerce에 모든 주문을 볼 수 있는 쇼트 코드/페이지가 있습니까? (0) | 2023.02.11 |
JObject를 사전으로 변환합니다. 가능합니까? (0) | 2023.02.07 |
Word press - get_results() - 실패 또는 비어 있는지 확인하는 방법 (0) | 2023.02.07 |
필요한 위치에 있지 않고 콘텐츠 상단에 쇼트코드가 표시됨 (0) | 2023.02.07 |