2016-02-04 43 views
1

我通過「REST API V2」插件向WordPress請求內容。這很好。還剩下一個問題:由「VisualComposer」插件創建的內容不會在de REST響應中呈現。WordPress「REST API」 - 渲染VisualComposer內容

的迴應是:

[vc_row]Hello World . . .[/vc_row] 

的反應應該是:

<div class="row">Hello World . . .</div> 

如何才能實現這一目標? 謝謝?

回答

1

這就是Visual Composer存儲內容的方式。如果禁用Visual Composer,則會看到它留下的所有內容都是您使用過的每篇文章中的一系列簡碼。在返回存儲的內容之前,WP REST API不執行簡碼。如果您不想在檢索內容後轉換內容,您可能需要查看編寫純HTML的頁面構建器,而不是依賴簡碼,也可以爲運行簡碼的WP REST API創建自定義端點返回HTML。

看起來他們有一個類似於我在WP.com API中推薦的端點,但在WP REST API中沒有類似的東西,AFAIK

如果你想去那條路線,頁面生成器插件here有一個很好的例子。

2

我已經通過使用另一個REST插件(JSON API)解決了這個問題。這個插件按預期呈現響應。 VisualComposer簡碼現在用HTML格式。

+0

請問? –

+0

我不再使用該接口,但我認爲它必須可以使用wordpress REST服務。 – mittererr

4

我認爲你可以用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; 
} 

編輯

這裏是鏈接的register_rest_field()功能:插件,你用什麼register_rest_field()

+0

對我不起作用 – Imran

+0

對不起聽到...也許你可以在我的回答開始時在github鏈接上發佈你的問題:) –

+0

我需要把這段代碼放在哪裏? – DD77