[英]Display users as HTML table
I want to display users of wordpress as a table just like https://stackoverflow.com/users 我想将wordpress用户显示为表格,就像https://stackoverflow.com/users
The recommended function is wp_list_authors, but it is not clear how can customise the layout. 推荐的功能是wp_list_authors,但尚不清楚如何自定义布局。
Say I want to display users in cells of table 5 x 5 with get next page link. 假设我要在具有“获取下一页”链接的表5 x 5中显示用户。
Please advice. 请指教。
Let me explain a bit. 让我解释一下。
Unlike get_users_of_blog wp_list_authors does not return an array. 与get_users_of_blog不同, wp_list_authors不返回数组。 If it would - then having an array I can build any table using foreach
. 如果可以的话-有一个数组,我可以使用foreach
建立任何表。 But wp_list_authors builds anchor tags on its own and returns monolith html block. 但是wp_list_authors自己构建锚标记,并返回整体式html块。 The only option to control layout would be passing some sort before and after tags. 控制布局的唯一选择是在标签前后传递某种排序。 But this function does not provide this sort of functionality. 但是此功能不提供此类功能。
As far as i know, there's no function like get_authors()
, but you can do it by a raw SQL query in a custom template: 据我所知,没有像get_authors()
这样的函数,但是您可以通过自定义模板中的原始SQL查询来实现:
Update: For pagination 更新:用于分页
I'm not sure if you can use the built-in WordPress pagination to do that, as the paged
param only appear to posts. 我不确定您是否可以使用内置的WordPress分页来执行此操作,因为paged
参数仅显示在帖子中。 You could fill a global $post var in a loop or something... There's a lot of approaches, but let's go for the "PHP" one. 您可以在一个循环中填充全局的$ post var之类的东西...有很多方法,但让我们选择“ PHP”方法。 =D = D
<?php
$i = 0;
$limit = 25;
$offset = ($o = trim($_GET['offset'])) ? $o : 0;
$users = $wpdb->get_results("SELECT user_nicename ... FROM {$wpdb->users} LIMIT $offset,$limit");
?>
<?php foreach ($users as $user) : ?>
<div class="someclass">
<?php echo $user->user_nicename; ?>
</div>
<?php if ($i++ % $number_of_columns == 0) : ?>
<div class="padder"></div>
<?php endif; ?>
<?php endforeach; ?>
To simulate the table appearance, just float "someclass" left and put a fixed width on it. 为了模拟表的外观,只需向左浮动“ someclass”并在其上放置固定的宽度即可。 The "padder" div (float left and width 100%) will ensure that the cells will be aligned by the highest one in that row. “ padder” div(浮动左侧,宽度100%)将确保该单元格与该行中的最高单元格对齐。
And for the pagination links: 对于分页链接:
<?php $n = $wpdb->get_var("SELECT count(ID) FROM {$wpdb->users}"); ?>
<?php $o = $offset - $limit; ?>
<?php if ($offset > 0) : ?>
<a class="prev" href="?offset=<?php echo $offset - $limit; ?>">Previous</a>
<?php endif; ?>
<?php $o = $offset + $limit; ?>
<?php if ($o < $n) : ?>
<a class="next" href="?offset=<?php echo $o; ?>">Next</a>
<?php endif; ?>
Code from brain to keyboard. 从大脑到键盘的代码。 Not tested, again. 再次未经测试。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.