我一直在嘗試使用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');
讓我試試 – Landry
它不起作用我也試圖讓這個網站的帖子像這樣wp_remote_get('http://domain.com/new/wp-json/wp/v2/posts') ;並且它仍然不起作用,其中domain.com是活網站的名稱。但是當我在本地站點上爲本地主機執行相同操作時,它至少會獲取本地站點的帖子並在本地站點上顯示使用簡碼 – Landry
這可能是由於活動站點的設置 - 它可能導致wp_remote_get()被禁用爲安全 – mike510a