如何为wordpress创建facebook feed?

时间:2014-09-18 18:35:23

标签: php json facebook wordpress facebook-graph-api

我是worpdress的新手,并完成了wordpress课程的基本开发,我们的最终项目是将状态和图片等facebook页面数据显示在word新闻网站上,特定要在页面中列出,我一直在研究使用Facebook开发人员并发现,在查询此网址时,https://www.facebook.com/feeds/page.php?id=[pageID]&format=json y获得了所有de数据的JSON,我也在测试过http://jsonviewer.net/看起来不错,不会让我陷入如何让JSON显示在我网站的网页上。

请提供一些帮助,

1 个答案:

答案 0 :(得分:1)

您可以为[fb-page id="ID-NUM"]使用Shortcode,使用函数wp_remote_get()提取Feed,然后使用PHP {{3}将返回的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' ) ); ?>