So let’s get down to it. I’ll just post the full PHP code here and then explain some of it after.
PHP author loop code that links to each author page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $args = array( 'meta_query' => array( array( 'who' => 'authors' // List only authors, not other users ) ), ); $site_url = get_site_url(); //get the site url // Create the WP_User_Query object $wp_user_query = new WP_User_Query($args); // Get the results $authors = $wp_user_query->get_results(); // Check for results if (!empty($authors)) { // loop trough each author foreach ($authors as $author) { echo '<div class="author">'; $author_info = get_userdata($author->ID); $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Author Bio for %s"), $author->display_name) ) . '">'; // Create the link to their author page echo $link; echo get_avatar( $author->ID, 128 ); echo '</a>'; echo '<h2><a href="' . $author_link . '">'.$author_info->first_name.' '.$author_info->last_name . ' ' . '-' . ' ' . get_user_meta($author->ID, 'title', true) .'</a></h2>'; echo '<p>'; echo word_count(get_user_meta($author->ID, 'description', true), '30'); echo '... <a href="'; echo $author_link; echo '">View Full Profile</a></p></div><!-- .author -->'; } } else { echo 'No authors found'; } ?> |
In the first section we are really just setting up our variables and beginning the query. You can define $args for any arguments you want applied to your query. Currently I’m just using ‘who’ => ‘authors’ to specify that we only want authors, not other users. For a more complete list of options for the query, check out the WordPress page on the WP User Query.
Then we actually have a foreach loop, which goes through each author and displays them along with a link to their page, some information, their avatar, and 30 words of their description in this case. If you need any help customizing this, let me know.
Creating the link to the Author Page
I would argue this is maybe the trickiest part to figure out. There is little documentation on this and you would THINK that there would be an obvious built-in function to do it. There IS a function that will list all the authors and link to their pages, wp_list_authors, but it isn’t very customizable and not nearly versatile enough for a full author-results template page. But when I took a look at the code behind wp_list_authors, I found get_author_posts_url which seems to get the job done.
1 2 |
$link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Author Bio for %s"), $author->display_name) ) . '">'; // Create the link to their author page echo $link; |
That should really be all you need! Other than that, this pretty much works like any other loop. You can add any custom HTML, formatting, or author author information in the foreach loop to customize as needed. Using this, you can create a fairly robust author results page similar to any post results page.
8 Comments on “Authors Loop with Links to Pages – WordPress Tutorial”
hi Brian! i appreciate the article and the code! i am new to php but am solid in html, css, and beginning js.
what i am trying to accomplish is to have the ‘next’ and ‘previous’ buttons on the posts cycle through only posts based on the author of the post. essentially looking to create multiple blogs under one roof that only cycle with themselves and not all posts available. i have 3-4 ‘sub-blogs’ my customer wants to run but not have them pollute each other.
i am sure this has been dealt with and i have searched for a solution that makes sense but i am coming up empty (or looking in the wrong corner)
any help or direction would be greatly appreciated!
jamie
Would it be possible to have an URL structure not with /author/authorname – But ex: /companies/companyname ?
Probably, I’d think you would need to edit your .htaccess file and add in a rewrite rule for it. There is lots of documentation for that available on the web!
Is there a way to do this and not pull in the admin profiles?
You would want to change your $args array depending on what you want. If you want to target a specific user level, you could use this:
$args = array( 'role' => 'Author' )
And then it would presumably only query users of that role. Note, that with just this code, it will even pull in author users who have no posts.
That worked. You are a lifesaver. Thank you!
Hey used this code snippet in my blog and it works like a charm. But now i want to display the authors website, do you know how this is done?
I tried several things but with no luck
You should be able to place this in the PHP where you want it to display:
the_author_meta('user_url',$author->ID);
Removing the $author->ID part along with that comma may work too, I haven’t tested it.