好的,我根据בניית אתרים\'这里有一个解决方案,因为我对此也很感兴趣。
首先,您需要获取博客ID列表,然后get_blog_list()
已弃用,因为它似乎是“suicidal database query“:)无论如何,看起来WP 3.2中会有一个替代方案称为wp_get_sites(). So使用this 功能。我建议你通过\'sort_column => \'last_updated\'
参数,以及\'limit\'
结果是20分左右。这将使下一个查询更快。
因此:
global $wpdb;
$number = 20; // maximum number of comments to display
$selects = array();
foreach (wp_get_sites() as $blog)
// select only the fields you need here!
$selects[] = "(SELECT comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_content, post_title, {$blog[\'blog_id\']} as blog_id FROM {$wpdb->base_prefix}{$blog[\'blog_id\']}_comments
LEFT JOIN {$wpdb->base_prefix}{$blog[\'blog_id\']}_posts
ON comment_post_id = id
WHERE post_status = \'publish\'
AND post_password = \'\'
AND comment_approved = \'1\'
AND comment_type = \'\'
ORDER BY comment_date_gmt DESC LIMIT {$number})"; // real number is (number * # of blogs)
$comments = $wpdb->get_results(implode(" UNION ALL ", $selects)." ORDER BY comment_date_gmt DESC", OBJECT);
然后渲染输出:
<ul>
<?php
$count = 0;
foreach((array)$comments as $comment):
$count++;
if($count == $number+1) break;
?>
<li>
<?php echo get_avatar($comment->comment_author_email, 32); ?>
<a href="<?php echo get_blog_permalink($comment->blog_id, $comment->comment_post_ID); ?>" title="commented on <?php echo strip_tags($comment->post_title); ?>">
<?php echo $comment->comment_author; ?> wrote:
<?php echo convert_smilies(wp_trim_excerpt($comment->comment_content)); ?>
(<?php echo human_time_diff(strtotime("{$comment->comment_date_gmt}")); ?>)
</a>
</li>
<?php
endforeach;
?>
</ul>
您还应该缓存结果,并每隔10分钟左右刷新一次缓存。