Working on a Clients Membership Website, one of the requirements was to display the authors in the WordPress site. Today I will show you how I achieve to display the author the author list and without using a plugin. If you want to create a author contact form, I also created a tutorial about that.
There may be other way, or proper way to do it. But this one works for me, if there are WordPress ninjas out there hope you can spare some time to correct my code.
My first choice was to use
<?php wp_list_authors( $args ); ?>
because the WordPress codex says:
Displays a list of the sites’s authors (users), and if the user has authored any posts, the author name is displayed as a link to their posts. Optionally this tag displays each author’s post count and RSS feed link.
But there is a problem; this doesn’t display the user photo or gravatar which is not good for the author list. Bavotasan wrote a wonderful custom function to list all authors with gravatars or user photo, and it is working fine until I tried to sign up on the website and my name was shown on the advisors list. And that’s when I though I needed a new function that will only show the real advisors.
Upon searching Google, I ended up looking on Codex and saw this
<?php get_users( $args ); ?>
According to the definition: Retrieves an array of users matching the criteria given in $args
.
<ul> <?php $blogusers = get_users('blog_id=1&orderby=nicename&role=subscriber'); foreach ($blogusers as $user) { echo '<li>' . $user->user_email . '</li>'; } ?> </ul>
Now that’s a relief! But it doesn’t fit the website needs; I need to display the author with gravatar or user photo. I tried to look at Bavotasan code and try to modify it a little.
Here is what I came up with:
<?php $blogusers = get_users('blog_id=1&orderby=nicename&role=author'); foreach ($blogusers as $user) { echo "<li>"; echo "<a href="".get_bloginfo('url')."/?author="; echo $user->ID; echo "">"; echo userphoto($user->ID); echo '<p>'; the_author_meta('display_name', $user->ID); echo "</p></a>"; echo “</li>"; } ?>
I will try to explain the code, I used the codex sample and mix it up a little.
$blogusers = get_users('blog_id=1&orderby=registered&role=author'); foreach ($blogusers as $user) {
In this code we set the parameters to authors only.
echo "<li>"; echo "<a href="".get_bloginfo('url')."/?author="; echo $user->ID; echo "">"; echo userphoto($user->ID);
This code will grab the url of our author ID, so if our user click the author image they will be directed to the author’s page.
the_author_meta('display_name', $user->ID);
This one display’s the author’s name.
You can also sort the authors depending on your needs, mine was to sort them from the clients order; so what I did is change the nicename to registered. Here are the choices to sort by ‘nicename‘, ‘email‘, ‘url‘, ‘registered‘, ‘display_name‘, or ‘post_count‘.
How to use
If you want to use this, just create a new page template and paste the code above. Guys at WPBeginner wrote an awesome tutorial about that. As for the CSS, this will do. Make sure that you wrap the code with <div class=”advisor_list”>.
.advisor_list{ overflow:hidden; } .advisor_list img{ vertical-align:top; margin:0 10px 10px 0; float:left; background:#fff; padding:5px; border:1px solid #ccc; width:90px; height:90px; } .advisor_list ul{ display:block; width:100%; overflow:hidden; } .advisor_list li{ width:100%; display:block; float:left; padding:20px 0; }
I think this is really useful for websites that wanted to display author’s list in wordpress without relying on a plugin. Also you show your author’s list if you put this on a sidebar, make sure that you are using Php Code Widget by Otto.
How do you display your author’s profile in your website? Do you use plugins or custom functions as well? Feel free to add them on the comments!