Support » Fixing WordPress » Get $author outside the loop

  • Resolved joshkadis


    After showing the content of a post, I’d like to show excerpts of a few other posts by the same author. I’ve placed this query in single.php after The Loop:

    <?php $author = get_query_var('author'); query_posts("author=$author&cat=64&showposts=4"); ?>

    The cat and showposts parameters are working but author is not – it just shows 4 posts in category 64 by any author). Incidentally, <?php the_author(); ?> works outside The Loop when placed just before the query.

    Is there any other way to do this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • In your single.php

    Put this in your loop:


    Then this after your loop:

       'showposts' => 3,
       'author' => $author_id,
    if ($posts) {
      $curuser = get_userdata($author_id);
      $author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
      echo 'User nicename: '.$curuser->user_nicename .', display Name: '. $curuser->display_name . ', link to author posts <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->user_nicename .'</a></p>';
      foreach($posts as $post) {
        setup_postdata($post); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
      } // foreach($posts
    } // if ($posts

    Thank you, that worked perfectly on single.php.

    When I tried using it on other post templates (via this plugin:, it doesnt’ show the nicename, display name, or link to author posts, and it shows three links to the current post instead of three other posts by the current post’s author.

    Any idea why this would be the case?

    Thank you.

    Guess you’d might have to assign the correct value to the $author_id variable I had placed in the loop.

    I’m not sure I follow, sorry. Do you mean manually assign the value? I’m working on a multi-author blog so that wouldn’t be an ideal solution.

    Not manually, but you may need to determine what variable does hold the post author ID and assign it to the $author_id variable.

    Do you mean this?

    $author_id=$post->[something other than post_author];

    Sorry, not the coding-est guy around. Thanks.

    If it is a ‘standard’ loop, then $post->post_author should work but depends on what is holding your posts.

    it could be:

    Cool. Thanks again for your help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get $author outside the loop’ is closed to new replies.