WordPress"REST API" - 渲染VisualComposer内容

mit*_*err 2 wordpress

我正在通过"REST API V2"插件从WordPress请求内容.这非常有效.只剩下一个问题:"VisualComposer"插件创建的内容不会在de REST Response中呈现.

回应是:

[vc_row]Hello World . . .[/vc_row]
Run Code Online (Sandbox Code Playgroud)

回应应该是:

<div class="row">Hello World . . .</div>
Run Code Online (Sandbox Code Playgroud)

怎么能实现?谢谢?

muk*_*ely 8

我想你可以在WP REST API v2中找到答案:https://github.com/WP-API/WP-API/issues/2578

以下示例来自上面的链接(谢谢你,bradmsmith!)

以下是如何在帖子内容上呈现VC短代码的示例:

add_action( 'rest_api_init', function ()
{
   register_rest_field(
          // if you need it to work with other (even custom post) types,
          // then you have to use an array:
          // array( 'page', 'post', 'custom_post_type', 'etc' )
          // this example only does the trick for 'page'
          // look at the link in the first EDIT section of this answer
          '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 );

   return $output;
}
Run Code Online (Sandbox Code Playgroud)

编辑

这是register_rest_field()函数的链接:register_rest_field()