Creating an Author Template in WordPress

Wednesday, August 26th 2009

Each user needs a landing page that displays all the user meta from her profile. Thankfully, WordPress has already accounted for this, because when the application requests an author by ID (?author=#), it looks for the following templates in this order:

  1. author.php
  2. archive.php
  3. index.php

All we need to do is make an Author template:

<?php get_header(); ?>

<div id="content" class="narrowcolumn">

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

    <h2>About: <?php echo $curauth->nickname; ?></h2>
    <dl>
        <dt>Website</dt>
        <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
        <dt>Profile</dt>
        <dd><?php echo $curauth->user_description; ?></dd>
    </dl>

    <h2>Posts by <?php echo $curauth->nickname; ?>:</h2>

    <ul>

    <!-- The Loop -->

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
            <?php the_title(); ?></a>,
            <?php the_time('d M Y'); ?> in <?php the_category('&amp;');?>
        </li>

    <?php endwhile; else: ?>
        <p><?php _e('No posts by this author.'); ?></p>

    <?php endif; ?>

    <!-- End Loop -->

    </ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Pretty nifty, huh? It even has the kindness to display a loop of posts authored by the user. All this thing is doing is dumping the user's meta into $curauth, so that you can request individual profile fields by echoing
"$curauth->name_of_your_field;" this will come in handy later when we make our own custom meta.

Next up, Integrating User Photo with Members List »

Your Comments

Much Ado About Nothing

2 comments

rss feed

  1. pcarlson Monday, March 8, 2010 at 9:30 pm

    This was the fifth page I looked at on author pages, and by far the most helpful. Many thanks!

Speak Your Mind

For Scathing Rebuttals

Enclose code in <code></code> brackets.