WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get $author outside the loop (9 posts)

  1. joshkadis
    Member
    Posted 5 years ago #

    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?

  2. MichaelH
    Member
    Posted 5 years ago #

    In your single.php

    Put this in your loop:

    <?php
    $author_id=$post->post_author;
    ?>

    Then this after your loop:

    <?php
    $args=array(
       'showposts' => 3,
       'author' => $author_id,
       'caller_get_posts'=>1
    );
    $posts=get_posts($args);
    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>
        <?php
      } // foreach($posts
    } // if ($posts
    ?>
  3. joshkadis
    Member
    Posted 5 years ago #

    Thank you, that worked perfectly on single.php.

    When I tried using it on other post templates (via this plugin: http://wordpress.org/extend/plugins/templates-for-posts/), 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.

  4. MichaelH
    Member
    Posted 5 years ago #

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

  5. joshkadis
    Member
    Posted 5 years ago #

    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.

  6. MichaelH
    Member
    Posted 5 years ago #

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

  7. joshkadis
    Member
    Posted 5 years ago #

    Do you mean this?

    '<?php
    $author_id=$post->[something other than post_author];
    ?>'

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

  8. MichaelH
    Member
    Posted 5 years ago #

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

    it could be:
    $author_id=$somethingelse->post_author

  9. joshkadis
    Member
    Posted 5 years ago #

    Cool. Thanks again for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic