doudeng2184 2014-06-06 19:42
浏览 44
已采纳

在WordPress中的PHP函数内调用FQL子查询

I have used the below function in my functions.php file in WordPress which is used to call the specified subqueries to show up a particular link's total facebook shares, likes, comments etc.

function fb_echoes($type) {
global $post;
$url = get_permalink($post->ID);

$fbconfig['appid' ]  = "1427040634231404";
$fbconfig['secret']  = "49a95c1eb6f7862caa8f66b04c87318d";
include_once "facebook.php";

$facebook = new Facebook(array(
    'appId'  => $fbconfig['appid' ],
    'secret' => $fbconfig['secret'],
    'cookie' => true
));

$fql = "SELECT url, normalized_url, share_count, like_count, comment_count, total_count, commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url = '".$url."' ";
$param  =   array(
    'method'    => 'fql.query',
    'query'     => $fql,
    'callback'  => ''
);
$fqlResult   =   $facebook->api($param);
return $fqlResult[0][$type];
}

To show the number of facebook comments anywhere in the page, I'm using this code:

<?php echo fb_echoes(commentsbox_count) ?>

I've already disabled the default WP comments but the small count icon on the posts and homepage shows the counts of WP comments. I want to show FB comments count over there. The below code displays the default comments count:

function it_get_comments($args) {
extract($args);
$tooltip = '';
$comments = get_comments_number($postid);
$label_text=__(' comments',IT_TEXTDOMAIN);
if($comments==1) $label_text=__(' comment',IT_TEXTDOMAIN);
if(empty($tooltip_hide)) $tooltip = ' info-bottom';
$out='<span class="metric' . $tooltip . '" title="'.__('Comments',IT_TEXTDOMAIN).'">';
if($comments>0 || $showifempty) {
    if($anchor_link) '<a href="#comments">';
        if($icon) $out.='<span class="icon icon-commented"></span>';
        $out.='<span class="numcount">' .$comments;
        if($label) $out.=$label_text;
        $out.='</span>';
    if($anchor_link) '</a>';
}
$out.='</span>';
return $out;
}

How do I call the commentsbox fql subquery into this function and override the WP comment counts over FB comments count!?

  • 写回答

1条回答 默认 最新

  • doushi5913 2014-06-07 00:53
    关注

    The get_comments_number() function filters the results through the filter get_comments_number before returning them, so if you hook into this filter you will be able to override the values with the results of your Facebook function.

    function facebook_get_comments_number( $count, $post_id ){
        return fb_echoes( 'commentsbox_count' );
    }
    add_filter( 'get_comments_number', 'facebook_get_comments_number', 10, 2 );
    

    Also not that while your code will work you should use single quotes around your arguments to fb_echoes() or PHP will see it as a constant, and following it with a semicolon is good form as well:

    <?php echo fb_echoes( 'commentsbox_count' ); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘