我一直在尝试使用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