我正在使用WordPress REST API在外部应用程序中获取WordPress页面的HTML内容.我正在调用这个mysite/wp-json/wp/v2/pages/10并返回:
"content": {
"rendered": "[vc_column_text]Hello World[/vc_column_text]"
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在它的最终HTML输出中返回代码而没有[vc_]短代码,例如: <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;
}
Run Code Online (Sandbox Code Playgroud)
评论中出现了一个问题:如何获取页面(帖子等)的自定义CSS集?我修改了示例代码,将自定义 CSS 添加到 REST API 响应中。您将在 中找到 CSS content/yb_wpb_post_custom_css。
另一种方法是向包含此 CSS 的 REST API 响应添加另一个字段。关键是为页面/帖子/等设置的自定义 CSS。有一个元键_wpb_post_custom_css。
| 归档时间: |
|
| 查看次数: |
4499 次 |
| 最近记录: |