开发者

Wordpress comments - perform simple math?

I have a Wordpress website in which one of the categories I've stripped the comments field down to basic whole numbers only. The logged in user can enter data on the post via the "comments" field, in basic simple whole numbers. Is there any wa开发者_JAVA百科y to take the MOST RECENT comment on all posts from that category (cat 1) and run basic math? My goal is to take the most recent comment's input from each post and add it together and display it via some php/html on another page/post or widget somewhere. Thanks!


If I'm understanding you right:

// 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜