简体   繁体   English

WordPress插件-通过帖子数对用户进行排名

[英]Wordpress plugin- rank users by number of posts

I want to create a simple plugin for wordpress -rank users based of number of user posts. 我想根据用户帖子数为wordpress -rank用户创建一个简单的插件。 Already i have this code and i don't know how to sort array and display it in table 我已经有了这段代码,而且我不知道如何对数组进行排序并将其显示在表格中

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);
}

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

You could iterate through the WPDB object and do whatever logic you need and put values into an associative array. 您可以遍历WPDB对象并执行所需的任何逻辑,然后将值放入关联数组中。 Sort it using the appropriate function from the PHP docs linked 使用链接的PHP文档中的适当功能对其进行排序

Displaying data from an array in an html table would look something like this: 从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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM