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>
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: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:
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 theYou can access the authors link as so:
Try this :
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
with
and it will work.