Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have used this with "echo" as its outside the loop i believe to the author of the post as a link, however its not displaying as a link?

<h2 class="sidebarheaders">Random Posts By You </h2>
<br/>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'date' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
Written by: 
<?php echo the_author_link(); ?>
<?php endforeach; ?>

Can someone advise?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You have to use setup_postdata function if you want to use the_author_link with custom loop. Do it like this:

<h2 class="sidebarheaders">Random Posts By You </h2>
<br/>
<?php
$rand_posts = get_posts( array( 'numberposts' => 5, 'orderby' => 'date' ) );
foreach( $rand_posts as $post ) : 
    setup_postdata( $post );

    ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    Written by: <?php echo the_author_link(); 

endforeach;

wp_reset_postdata();
?>

Don't forget to call wp_reset_postdata to restore the $post global to the current post in the main query.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.