doushifen4060 2016-02-16 12:06
浏览 132

从WordPress REST API获取原始HTML输出

I'm using the WordPress REST API to get the HTML content of my WordPress page in an external application. I'm calling this mysite/wp-json/wp/v2/pages/10 and it returns:

"content": {
  "rendered": "[vc_column_text]Hello World[/vc_column_text]"
}

Is there any way to return the code in it's final HTML output and without the [vc_] shortcodes, eg: <p>Hello World</p>

The shortcodes are coming from the Visual Composer page builder plugin.

  • 写回答

2条回答 默认 最新

  • doushua7737 2016-08-17 18:07
    关注

    Found and answer here: https://github.com/CompassHB/web/issues/67#issuecomment-245857301

    The example below is taken from the link above:

    /**
     * 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 );
    
       return $output;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮