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

发布于 2024-09-05 12:53:15 字数 176 浏览 2 评论 0原文

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

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 way 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-09-12 12:53:15

如果我理解正确的话:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文