【问题标题】:Wordpress comments - perform simple math?Wordpress 评论 - 执行简单的数学运算?
【发布时间】:2011-03-02 21:31:26
【问题描述】:

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

【问题讨论】:

    标签: php wordpress comments wordpress-theming


    【解决方案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;
    

    【讨论】:

    • 这很完美!我最终将它实现为一个 wordpress 管理仪表板小部件,这样它对于运行该网站的人来说是“只看到”的!
    猜你喜欢
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多