[英]How to retrieve content from local site to live wordpress site using wp rest api
我一直在嘗試使用wp rest api使用shortcode將數據從本地站點拉到實時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');
這是因為“ localhost”不是有效的主機名-您必須通過公共IP地址訪問帖子。
在任何搜索引擎中輸入“我的IP是什么”,它將為您提供一系列由點組成的4個數字,
例如: 124.98.221.75
然后,將localhost
替換為124.98.221.75
所以會的
$response = wp_remote_get( 'http://124.98.221.75/wordpress/wp-json/wp/v2/posts' );
您可以將?page = {$$ page_number}添加到所請求的URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.