Php 从WordPress REST API获取原始HTML输出
我正在使用获取外部应用程序中WordPress页面的HTML内容。我将其命名为mysite/wp json/wp/v2/pages/10,它返回:Php 从WordPress REST API获取原始HTML输出,php,json,wordpress,api,rest,Php,Json,Wordpress,Api,Rest,我正在使用获取外部应用程序中WordPress页面的HTML内容。我将其命名为mysite/wp json/wp/v2/pages/10,它返回: "content": { "rendered": "[vc_column_text]Hello World[/vc_column_text]" } 有没有办法在它的最终HTML输出中返回代码而不使用[vc_u2;]短代码,例如:Hello World 短代码来自。在此处找到并回答: 下面的示例取自上面的链接: /** * Modify RES
"content": {
"rendered": "[vc_column_text]Hello World[/vc_column_text]"
}
有没有办法在它的最终HTML输出中返回代码而不使用[vc_u2;]短代码,例如:Hello World
短代码来自。在此处找到并回答:
下面的示例取自上面的链接:
/**
* 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?我修改了示例代码,使其将自定义CSS添加到REST API响应中。您可以在content/yb\u wpb\u post\u custom\u CSS
中找到CSS
另一种方法是向包含此CSS的REST API响应添加另一个字段。关键是,页面/帖子/等的自定义CSS集有一个元键\u wpb\u post\u custom\u CSS,但是,以下内容对我有用:
$output['rendered'] = apply_filters( 'the_content', get_the_content() );
以防万一,如果有人想知道。这里也有同样的问题。我一直在尝试使用内容过滤器将其转换为HTML。我也在支持论坛上发了帖子,所以我希望在那里或这里得到回复。:)如何获取CSS,rest api没有Visual Composer提供的CSS?@GeorgeAlvis:我修改了我的答案,所以现在它会提供一个包含自定义CSS的rest响应。WPB将自定义CSS存储在元数据中,而不是存储在内容中,这就是为什么您没有在请求内容时获得它。meta可以一个接一个地添加,我就是这么做的。
$output['rendered']=apply_filters('the_content',get_the_content())代码>谢谢你的回答,我正在从上面的行中检索页面内容,但是现在我也希望在api中使用页面CSS,而不是自定义CSS。我该怎么做?它位于答案的编辑部分-向内容添加新字段,或向REST响应添加新字段。我不使用自定义CSS,但希望visual composer元素CSS在API中可用@穆卡·格格利