Wordpress评论 - 执行简单的数学运算?

时间:2010-06-11 23:33:51

标签: php wordpress comments wordpress-theming

我有一个Wordpress网站,其中一个类别我将评论字段仅删除到基本整数。登录用户可以通过“评论”字段以基本的简单整数输入帖子上的数据。有没有办法对该类别(猫1)中的所有帖子进行最近评论并运行基本数学?我的目标是从每个帖子中获取最新评论的输入并将其添加到一起并通过某些php / html在另一个页面/帖子或小部件上显示它。谢谢!

1 个答案:

答案 0 :(得分:1)

如果我理解你的话:

// http://codex.wordpress.org/Function_Reference/query_posts
$args = array(
    'cat' => 1,
    'orderby' => 'date',
    'order' => 'desc',
    'posts_per_page' => -1,
);
// get all (-1) posts in category 1
$posts = query_posts($args);

// variable to hold our basic sum
$sum = 0;
foreach($posts as $post) {
    $post_id = $post->ID;
    // http://codex.wordpress.org/Function_Reference/get_comments
    $comment_args = array(
        'post_id' => $post_id,
        'status' => 'approve',
        'orderby' => 'comment_date_gmt',
        'order' => 'DESC',
        'number' => 1,
    );
    // get the most recent approved comment by post_id
    $comments = get_comments($comment_args);
    foreach($comments as $comm) {
        // set the integer value of the comment's content
        $integer_comment_value = (int)$comm->comment_content;
        // add it to the sum
        $sum += $integer_comment_value;
    }
}
// print the sum
print $sum;