0

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?

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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