WordPress主题制作:添加一个用户列表页面模板(含用户头像)

如果你的WordPress站点是开放注册的,拥有众多作者和注册用户,你可能需要将他们罗列出来,下面倡萌就简单介绍下如何制作一个WordPress用户列表页面模板(含用户头像)。

1.在主题根目录新建一个名为 page-user-list.php 的文件,然后复制你主题的 page.php 的代码到 page-user-list.php ,并且在最上方添加:

<?php
/*
Template Name: User List
*/
?>

2.在 page-user-list.php 找到

<?php the_content(); ?>

在其下方添加下面的代码:

<?php
     $display_admins = false; //是否显示管理员,ture - 显示,false -不显示
     $order_by = 'post_count'; // 排序依据,可选 'nicename', 'email', 'url', 'registered', 'display_name', 或 'post_count'
     $order = 'DESC'; //排列方式,ASC - 升序,DESC - 降序
     $role = ''; // 用户角色,可选 'subscriber', 'contributor', 'editor', 'author' - 留空显示全部 'all'
     $avatar_size = 161; //头像大小
     $hide_empty = false; // 是否隐藏没有文章的用户,ture - 显示,false -不显示
 
     if(!empty($display_admins)) {
          $blogusers = get_users('orderby='.$order_by.'&role='.$role);
     } else {
 
     $admins = get_users('role=administrator');
     $exclude = array();
 
     foreach($admins as $ad) {
          $exclude[] = $ad->ID;
     }
 
     $exclude = implode(',', $exclude);
     $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&order='.$order.'&role='.$role);
     }
 
     $authors = array();
     foreach ($blogusers as $bloguser) {
     $user = get_userdata($bloguser->ID);
 
     if(!empty($hide_empty)) {
          $numposts = count_user_posts($user->ID);
          if($numposts < 1) continue;
          }
          $authors[] = (array) $user;
     }
 
     echo '<ul id="grid-contributors">';
     foreach($authors as $author) {
          $display_name = $author['data']->display_name;
          $avatar = get_avatar($author['ID'], $avatar_size);
          $author_profile_url = get_author_posts_url($author['ID']);
 
          echo '<li class="single-item">';
          echo '<div class="author-gravatar"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
          echo '<div class="author-name"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></div>';
          echo '</li>';
          }
     echo '</ul>';
?>

请根据自己的实际需要,修改 2-7 行代码。

3.保存 page-user-list.php ,然后在后台 > 页面 > 新建页面,在“页面属性”的“模板”中选择“User List”模板,保存页面即可。

注:具体的前端样式就靠大家自己折腾CSS代码了。

参考资料:http://wp-snippet.com/snippets/author-list-with-avatar-thumbnail/

来源:

https://www.wpdaxue.com/user-list-with-avatar.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32259.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?