簡體   English   中英

WordPress插件-通過帖子數對用戶進行排名

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

我想根據用戶帖子數為wordpress -rank用戶創建一個簡單的插件。 我已經有了這段代碼,而且我不知道如何對數組進行排序並將其顯示在表格中

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中的數組排序: http//php.net/manual/zh/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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM