【发布时间】:2017-04-04 17:51:18
【问题描述】:
我一直在尝试使用 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');
【问题讨论】: