Display author with link (WordPress)

I’m trying to display my latest 3 blog posts on my website homepage. I’ve got everything so far (Content, Image and Date). However it’s not allowing me to display the authors name with link so that people can click on the author and see what their latest posts have been. Does anybody know why this is happening?

My code so far:

<div class="col-sm-6">
<?php   
     $args = array( 'numberposts' => 2, 'offset' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
     $postslist = get_posts( $args );
     foreach ($postslist as $post) :  setup_postdata($post); 
?>
<div class="blog-post blog-media wow fadeInRight" data-wow-duration="300ms" data-wow-delay="100ms">
   <article class="media clearfix">
      <div class="entry-thumbnail pull-left">
         <div class="img-responsive smallBlogImage"><?php getTheFirstImage(); ?></div>
             <span class="post-format post-format-gallery"><img class="blogIconSmall" src="IsosecWeb/images/IsosecIcon.png"></span>
         </div>
         <div class="media-body">
         <header class="entry-header">
            <div class="entry-date"><?php the_date(); ?></div>
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
         </header>

         <div class="entry-content">
            <p><?php echo wp_trim_words(preg_replace("/< *[img][^>]*[.]*>/i","", get_the_content(), 80), 20); ?></p>
            <a class="btn btn-primary" href="<?php the_permalink(); ?>">Read More</a>
         </div>

         <footer class="entry-meta">
             <span class="entry-author"><i class="fa fa-pencil"></i> <?php echo the_author_posts_link(); ?> </span>
         </footer>
      </div>
   </article>
</div>
<?php  endforeach;  ?> 
</div>

Related posts

3 comments

  1. Explanation

    The reason why this is not showing up is because the_author_posts_link(); needs to be used in The Loop and only the loop.

    If you print_r($postslist) you will get an array back like so:

    Array ( [0] => Array ( [ID] => 1 [post_author] => 1 [post_date] => 2015-07-03 11:04:24 [post_date_gmt] => 2015-07-03 11:04:24 [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! [post_title] => Hello world! [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => hello-world [to_ping] => [pinged] => [post_modified] => 2015-07-19 21:11:24 [post_modified_gmt] => 2015-07-19 21:11:24 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost:8888/intelligence/?p=1 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 1 [filter] => raw ) )
    

    What you need to do is access the key values individually. So for example, to get the date, if you would access it like so:

    <?php echo postslist['post_date']; ?>
    

    The Author Link

    You will notice if you look at the post_author key, it returns an integer value, or the id of the admin. What we can do is then pass this into the

    <span class="entry-author">
         <i class="fa fa-pencil"></i>
         <?php the_author_meta( 'display_name', $postslist['post_author'] ); ?>
    </span>
    

    You can access the authors link as so:

    <a href="<?php echo get_author_posts_url( $postslist['post_author'] ); ?>">
        Posts by  <?php the_author_meta( 'display_name', $postslist['post_author'] ); ?>
    </a>
    
  2. Try this :

     <a href="<?php echo get_author_posts_url( $post->post_author); ?>"><?php echo get_the_author_meta( 'display_name',$post->post_author); ?></a>
    
  3. Your code looks pretty correct. The function “the_author_posts_link()” returns anchor tag – which links to all post by the author. There is no need to write “echo” to print it.

    You just need to replace

    <?php echo the_author_posts_link(); ?> 
    

    with

    <?php the_author_posts_link(); ?>
    

    and it will work.

Comments are closed.