Skip navigation

Using Author Template Tags Outside of the WordPress Loop

WordPress allows you to create a custom Author web page for your WordPress blog. You can make this page look like anything you want, customized to the author or with just some tidbits about the author added to the page to help readers learn a little more about the person behind the post.

I had very specific ideas about the look and what information I wanted on the author page for my new WordPress blog. Since I am planning on working with a variety of authors, I wanted to showcase each author’s contact information, home website, a description or bio of the author, and then a highlighted list of the articles the author has published on the blog. This turned into a bit more of a project than I planned, so I thought I would share my techniques with you.

If you want to create a custom author page in WordPress, start with the WordPress Codex article on WordPress Custom Author Template Files, good advice on how to create a custom author template file.

Typically, most WordPress blogs have only one author, therefore, most WordPress Themes, if they include a byline or author information, do so as plain text. WordPress has a template tag that can replace the standard author template tag to turn the author’s name into a link. Click the link and the visitor is taken to a web page that generates a “search result” listing of posts by that author. Very simple and automatic.

To create such an author link, Change the commonly used <?php the_author(); ?> template tag to:

<?php the_author_posts_link('namefl'); ?>

A link with the name of the author will be automatically generated, such as:

Lorelle VanFossen

You can add this template tag author link as a byline or in the post meta data section of your posts. When clicked, the link will go to the index.php template file to generate a list of posts written by that author, or, if you have it, use the author.php template file which you can customize to make it look however you want. I had very specific ideas on how I wanted my custom author template page to look.

Because I don’t do simple things like rely upon WordPress to just generate the author’s page by default, I wanted to create a customized author page with contact information and a biography for each author. I created a customized author.php template file which WordPress seeks automatically when an “author link” is clicked.

Remember, when working with default template hierarchy files, you don’t need to do anything. WordPress will search through a set of template files and if it finds it, it will automatically use it. If it doesn’t, it goes back to the default, which is the index.php template file. So, if it is there, it uses it. For more on how template files work in WordPress, see Stepping Into Templates.

On my custom author.php template file, I wanted the title to read Articles by Lorelle VanFossen, or whoever the author is. I also want a short list of the author’s main website, email contact, and biography, helping readers learn more about the author. Then the WordPress Loop would begin, generating a list of posts by that author.

The template tags and text I wanted to use should have looked like this:

Articles by <?php the_author(); ?>
Author Website: <?php the_author_url(); ?>
Author Email: <?php the_author_email(); ?>
Author Bio: <?php the_author_description(); ?>

However, I have a little problem.

The template tags I wanted to use work only within the WordPress Loop, a bit of PHP code that gathers information from the database and displays it. By using these template tags outside of the Loop, the key that starts the process of gathering information from the database isn’t initiated, so there is nothing to find. They don’t work.

If I used these template tags within the WordPress Loop, so they will work, this information would be generated on each and every post returned written by the author. That’s too much duplicate information. I wanted to list this information separately from the post search results, and to only display once. Yet, these tags only work inside the Loop. I had to find another method of calling the information from the database outside of the WordPress Loop.

I found the solution in the WordPress Custom Author Template Files article on the WordPress Codex.

It begins by adding a GET call to the database (to GET data) placed just below the call to the header in the author.php template file. It creates a kind of mini-loop, gathering information about the author from the database and assigning it to variables which can then be used outside of the WordPress Loop:

<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin(get_the_author_login());
else :
$curauth = get_userdata(intval($author));
endif;
?>

In the area where I want the information about the author gathered from the database to display, I added the following, totally replacing the Loop template tags I wanted above:

<h2 class="posttitle">Articles by <?php echo $curauth->display_name; ?></h2>
<div id="authorinfo">
<p><strong>Author Website:</strong> <a href="<?php $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a><br />
<strong>Author Email:</strong> <a href="mailto:<?php echo antispambot($curauth->user_email); ?>">Contact Author</a><br />
<strong>Author Bio:</strong> <?php echo $curauth->description; ?></p>
</div>

Note that I changed the way the Author Contact information and tag is laid out. I took advantage of a WordPress built-in feature that hides the email address from bots and harvesters that steal email addresses for spam. If I was using the email tag within the Loop, it would be <a href="mailto:<?php echo antispambot(get_the_author_email()); ?>">Email Author</a>. Just for your information.

From there, the Author Template File goes on as usual, and the WordPress Loop begins, gathering information about the posts written by the author. It is not necessary to change the following in your author.php template file. This is just an example of how I laid out my author search results:

<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>" class="posttitle"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div id="searchinfo">By: <?php the_author_posts_link('namefl'); ?> | Categories: <?php the_category(', '); ?> | Comments: <a href="<?php the_permalink() ?>#comments" title="<?php the_title(); ?>" rel="bookmark"><?php comments_number('0','1','%','%'); ?></a></div><!– end searchinfo –>
<div class="entry"><?php the_excerpt(); ?> <div class="readmore"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">CONTINUE READING</a></div>
</div><!– end of entry –>…

The end results of my custom author.php template file for the section with the author details looks like this.

WordPress Customized Author Template File by Lorelle VanFossen

Thanks to the great folks who volunteer their amazing talents on the WordPress Codex for helping me figure this out!

For more WordPress Plugins and techniques for customizing author pages and the author template file, see WordPress Plugins for Multiple Blogger Blogs.

Related Articles



Site Search Tags: , , , , , , , , ,

Feed on Lorelle on WordPress Subscribe Feedburner iconVia Feedburner Subscribe by Email
Copyright Lorelle VanFossen, the author of Blogging Tips, What Bloggers Won't Tell You About Blogging.

11 Comments

  1. Posted May 7, 2006 at 8:31 pm | Permalink

    Thanks for the step-by-step explanation. I’m thinking that the concept here could be applied to create other specialized pages by switching out the tags.–Dan

  2. Posted May 7, 2006 at 9:25 pm | Permalink

    Nice article, thanks.

  3. Posted May 30, 2006 at 6:43 pm | Permalink

    Those of us who would rather not tinker with PHP will wait for a plugin or widget.

  4. Posted May 30, 2006 at 7:46 pm | Permalink

    Kafkaesquí’s Get Author Profile Plugin

  5. Posted February 27, 2007 at 11:27 am | Permalink

    I want to show up the authors with no posts (yet). Can u help ?

  6. Posted February 27, 2007 at 11:43 am | Permalink

    Empty Authors and Categories will not appear by default on a WordPress blog. If you need to know the number of posts for specific authors within your Administration Panels, check out one of the different statistics in Counting WordPress: Statistics WordPress Plugins for author statistics.

    Other than that, the best place to look for a custom script or request one would be in the WordPress Support Forum.

  7. johan
    Posted May 10, 2007 at 5:06 pm | Permalink

    Do you know how to get the Userextra and/or Usermeta plugins to work for the author.php page.
    I’ll filles in the adres value and want to print Extended User Data which is profided using the plugin:

  8. Posted May 10, 2007 at 8:34 pm | Permalink

    If you want the Plugins to work in the author.php, you have to put the Plugin template tags into the template file, author.php. Follow the instructions EXACTLY as put in the readme.txt or on the Plugin author’s website and that will allow you to add the meta information.

    If you need more help with the Plugins, please contact the Plugin author directly.

  9. Posted June 16, 2007 at 10:54 pm | Permalink

    thanks so much for this, it REALLY helped me out a lot on my author pages!

  10. Posted March 3, 2008 at 2:52 pm | Permalink

    Great Article! But how do I get the author image onto the author page?

  11. Posted March 3, 2008 at 3:30 pm | Permalink

    There are WordPress Plugins to create variables which include photographs of the authors onto the author.php template file. See WordPress Plugins for Multiple Blogger Blogs for examples.

3 Trackbacks/Pingbacks

  1. [...] http://lorelle.wordpress.com/2006/05/06/using-author-template-tags-outside-of-the-wordpress-loop/ [...]

  2. [...] I love challenges and recently I was challenged with a way to make information about a post author show up in a WordPress Theme outside of the WordPress Loop, which I explained in “Using Author Template Tags Outside of the WordPress Loop”. Another even challenging author issue came up with my genealogy blog. How to add dead authors to my blog authors and users. I uncovered the mysteries in “Blog Contributors - Wanted Dead or Alive”, and had some morbid fun while I was at it. In the future, I will use this information to create more customized author pages, so stay tuned for more on this. [...]

  3. [...] Using Author Template Tags Outside of the WordPress Loop by Lorelle [...]

Post a Comment

Your email is never published nor shared.