2016-11-21 73 views
0

我一直在嘗試使用wp rest api將數據從本地站點移動到使用簡碼的wordpress站點。當我試圖做相反的事情,即將帖子從實況轉移到本地時,它可以工作,但是當我將鏈接從本地更改爲實況時,它不會在頁面上顯示任何內容。如何使用wp rest api從本地站點檢索內容到wordpress站點

我已經在這兩個網站上安裝了wp rest api。下面是我的代碼:

function my_recent_posts_shortcode($atts){ 
$response = wp_remote_get('http://localhost/wordpress/wp-json/wp/v2/posts'); 
    if(is_wp_error($response)) { 
return; 
} 

$posts = json_decode(wp_remote_retrieve_body($response)); 
if(empty($posts)) { 
return; 
} 

if(!empty($posts)) { 
$list = '<ul class="recent-posts">'; 

foreach($posts as $post) { 
$list .='<li><a href="' . $post->link. '">' . $post->title->rendered . '</a> 
</li>'; 
} 
return $list . '</ul>'; 
} 
} 
add_shortcode('recent-posts', 'my_recent_posts_shortcode'); 

回答

0

這是因爲「localhost」的是不是一個有效的主機名 - 你必須通過你的公網IP地址來訪問的職位。

鍵入「什麼我的IP」到任何搜索引擎,它會給你一系列由點分隔的4個數字,

例如:124.98.221.75

然後,124.98.221.75

替換詞 localhost

因此,這將是

$response = wp_remote_get('http://124.98.221.75/wordpress/wp-json/wp/v2/posts');

+0

讓我試試 – Landry

+0

它不起作用我也試圖讓這個網站的帖子像這樣wp_remote_get('http://domain.com/new/wp-json/wp/v2/posts') ;並且它仍然不起作用,其中domain.com是活網站的名稱。但是當我在本地站點上爲本地主機執行相同操作時,它至少會獲取本地站點的帖子並在本地站點上顯示使用簡碼 – Landry

+0

這可能是由於活動站點的設置 - 它可能導致wp_remote_get()被禁用爲安全 – mike510a

0

您可以將?page = {$ page_number}添加到您請求的網址中。

例如,http://domain.com/wp-json/wp/v2/posts/?page=2

+0

中刪除分號請你能幫助我在這個鏈接上的另一個問題; – Landry

+0

是的,當然我會 –

+0

@ VasimVanzara這是我的問題http://stackoverflow.com/questions/40929020/post-request-to-custom-end-point – Landry