• Use Author Template and Get Author Data in WordPress

    Posted by markzero on Sunday, March 20th, 2011

    Often in posts you have the “posted by” part above or below article. To get author link, name or nickname (nicename) use following example:

     
    <?php
    <p class="author">posted by <a href="<?php the_author_link(); ?>">
    <?php the_author(); ?></a></p>
    ?>

    This will produce link like, example.com/author/name/. Now, make author.php file. For full explanation of template system refer to Author_Templates page. In this file get the author:

     
    <?php
    $curauth = (isset($_GET&#91;'author_name'&#93;)) ? 
    get_user_by('slug', $author_name) : 
    get_userdata(intval($author));
    ?>

    This is one of ways for getting author object in WordPress. Next, get any attribute of this object, like:

     
    <?php
    $curauth->user_nicename;
    $curauth->first_name;
    $curauth->last_name;
    ?>

    …etc… Now you can do anything with this object, even for the loop, for example, getting last two posts on this page:

     
    <?php 
    $query = new WP_Query( 'author_name='.$curauth->user_nicename.'&posts_per_page=2' );
    if( have_posts() ) :
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="video-wrap-author"><?php the_block('authors_video'); ?></div>
    <?php
    the_content();
    endwhile;
    endif;
    ?>

    If you have any issues/questions, write below.

    Posted in: PHP, Wordpress.

    Share this story:

    Leave a Reply