Display Multiple Authors In WordPress
Ever needed or wanted to know how to display more than one author’s posts on a WordPress blog such as the homepage? Thanks to the many functions within WordPress this is quite easy to do and requires only a few lines of code.
In order to show more than one author we are going to use the query_posts() function. In order for this to work correctly you need to place the code before the beginning of the code that starts The Loop. An example query_posts() fucntion would look something like the following.
<?php
query_posts('author_name=admin&showposts=5');
?>
In the above code you can see the variables that we set inside of the parenthesis and within single quotes; author_name and showposts. The author_name variable is the username for the author that you are wanting to display, not the first or last name (the username can be different from the display name). The showposts variable just sets how many posts you would like to have displayed. Pretty simple right?
The only thing left now is to add in the code used to check if there are posts, the code to display the post’s information and then close the loop itself.
<?php
query_posts('author_name=admin&showposts=5');
if(have_posts()) : while (have_posts()) : the_post();
?>
Code to display the post goes here
<?php endwhile; endif; ?>
The above code will allow you to use multiple loops on your WordPress blog in order to display as many author’s posts as you would like. All that you have to do is change the author_name variable to whatever author you would like to show.
Lets go ahead and write out a sample piece of code that would list 2 different authors.
<?php
query_posts('author_name=admin&showposts=5');
if(have_posts()) : while (have_posts()) : the_post();
?>
<div id="authoradmin">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
<?php
query_posts('author_name=bobo&showposts=5');
if(have_posts()) : while (have_posts()) : the_post();
?>
<div id="authorbobo">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
There you have it, the above code will display the latest 5 posts from the author with the username of admin and then display the latest 5 posts from the author with the username of bobo. You can use IDs for each author’s posts in order to give each author’s content their own custom look.
Feedback!
If you read this post and understood it great! Let me know in the comments below. If you are still having issues and can't get it figured out, you are more than welcome to leave a comment or send me an email using the contact form and I'll do what I can to help!
You rock! Thank you :)