我是worpress的新手,在wordpress课程中完成了一个基本的开发,我们的最后一个项目是带来一个facebook页面数据,如状态和图片,显示在word press网站上,具体地列在页面中,我一直在使用facebook开发人员进行研究,发现在查询这个url时,https://www.facebook.com/feeds/page.php?id=[pageID]&format=json我得到了带有所有de数据的json,我也在http://jsonviewer.net/看起来不错,不,我一直在纠结如何让JSON显示在我网站的页面上。
请需要一些帮助,
您可以使用该[fb-page id="ID-NUM"]
的Shortcode,使用函数wp_remote_get()
提取提要,然后使用PHP的json_decode()
将返回的JSON转换为数组。
add_shortcode( 'fb-page', 'shortcode_so_25919996' );
function shortcode_so_25919996( $atts )
{
if( empty( $atts['id'] ) )
return 'Please, provide an ID';
# Request URL content.
$url = 'https://www.facebook.com/feeds/page.php?id=' . $atts['id'] . '&format=json';
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) )
return 'Error fetching the feed.';
# Response OK. Decode the response body.
$json_to_array = json_decode( wp_remote_retrieve_body( $response ), true );
# Print the array as code block. Use a loop to build the output as HTML string.
return '<pre><code>' . print_r( $json_to_array, true ) . '</code></pre>';
}
或者您可以将此函数调用为:
<?php echo shortcode_so_25919996( array( 'id' => 'ID-NUM' ) ); ?>