【问题标题】:Wordpress plugin- rank users by number of postsWordpress 插件 - 按帖子数量对用户进行排名
【发布时间】:2018-10-03 21:55:18
【问题描述】:

我想根据用户帖子的数量为 wordpress 用户创建一个简单的插件。我已经有了这段代码,但我不知道如何对数组进行排序 并在表格中显示

global $wpdb; 
$result = count_users();
$users = $result['total_users'];

for($id = 1;$id<$users;$id++){ 
    $result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' 
        FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author 
        WHERE wp_posts.post_type = 'post'             
        AND wp_users.ID = $id", ARRAY_A);
}

【问题讨论】:

    标签: php mysql wordpress plugins


    【解决方案1】:

    PHP 中的数组排序:http://php.net/manual/en/array.sorting.php

    您可以遍历 WPDB 对象并执行您需要的任何逻辑并将值放入关联数组中。使用链接的 PHP 文档中的适当函数对其进行排序

    在 html 表中显示数组中的数据如下所示:

    <table>
       <tr>
           <th>Rank</th>
           <th>Username</th>
           <th>Post Count</th>
       </tr>
    <?php
    $i = 1;
    foreach($array as $value){?>
           <tr>
              <td><?php echo $i; ?></td>
              <td><?php echo $value['user']; ?></td>
              <td><?php echo $value['posts']; ?></td>
           </tr>
    <?php 
          $i++;
    } ?>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多