Authors Loop with Links to Pages – WordPress Tutorial

Authors Loop WordPress

Much like this roller coaster, a good WordPress Loop should be versatile, even for Authors

Creating the loop for a page that displays all of your authors in your WordPress website is something that’s not as straight-forward as you would think it would be. Additionally, actually linking to the author’s page is fairly tricky. So today I am going to show you how to do it. You can take the final code and put it in a page called page-pagename.php in your template folder, replacing “pagename” with the name you want to page to have. For more info on that, view the WordPress codex page about page templates.

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

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.

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.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

8 Comments on “Authors Loop with Links to Pages – WordPress Tutorial”

  1. 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

  2. Would it be possible to have an URL structure not with /author/authorname – But ex: /companies/companyname ?

    1. 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!

    1. 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.

  3. 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

    1. 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.

Leave a Reply

Your email address will not be published.