Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

标题可能稍微有点拗口,大致意思就是你发布文章同步到了微博,如果有人在微博给你评论了,那么评论就会同步到文章的评论中来。
标题可能稍微有点拗口,大致意思就是你发布文章同步到了微博,如果有人在微博给你评论了,那么评论就会同步到文章的评论中来。
如果删除微博评论可能造成神经错乱之类的bug,这里就不对这种情况进行考虑了。同时需要主机支持file_get_contents
首先你得按照这篇文章http://www.zzbang.cn/cms/wordpress/2572.html添加了文章同步微博,然后才可以实现这个功能。之前已经添加了这个功能的需要更新下代码才可以正常使用。授权方法不再重复了。
然后下面的代码加入到functions.php中
function weibo_comments(){
$post_id = get_the_ID();
$token = get_user_meta(1,"sina_access_token",true);//你的access_token
if(get_post_meta($post_id,'weibo_id', true)){
$args = array(
'post_id' => $post_id,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_from_weibo',
'value' => '1',
'compare' => '='
),
),
);
$commentssss = count(get_comments($args));
$get_user_info = "https://api.weibo.com/2/comments/show.json?id=".get_post_meta($post_id,'weibo_id', true)."&access_token=".$token;
$data = file_get_contents ( $get_user_info );
$str = json_decode($data , true);
$comment_array = array_reverse($str['comments']);//数组倒序
if(count($str['comments']) > $commentssss){
for($i = $commentssss;$i < count($comment_array);$i++ ){
$first = $comment_array[$i];
$comment_post_ID = $post_id;
$comment_author = $first['user']['name'];
$comment_author_url = $first['user']['url'];
$comment_content = $first['text'];
$comment_author_email = "";
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content');
$comment_id = wp_new_comment( $commentdata );
update_comment_meta($comment_id, '_from_weibo', 1);
update_comment_meta($comment_id, '_avatar', $comment_author_url = $first['user']['profile_image_url']);//保存头像
}
}
}else{
return false;
}
}
然后在single.php的loop中使用weibo_comments();调用即可
<?php
if(get_comment_meta(get_comment_ID(), '_avatar')){
echo '<img src="'.get_comment_meta(get_comment_ID(), '_avatar',true).'">';
}else{
echo get_avatar( $comment, $size = '40');
}
?>
每次访问都会抓取一下微博API,如果微博未同步过来的评论过多,可能会造成轻微影响。
原文 http://fatesinger.com/960