Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKonrad Pilch
2,435 PointsHow do i do this?
How do i solve this?
My link doesnt work. It goes to only one post, if it goes at all. It needs to loop like the recent posts : p but it doesnt work .
<div class="resent-posts">
<ul class="archives-ul">
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){ ?>
<a href="<?php the_permalink(); ?>">
<li><?php echo $recent["post_title"] ?> <br><small class="entry-date" datetime=""><?php echo get_the_date( $format, $post_id ); ?></small></li>
</a>
<?php } ?>
</ul>
</div>
1 Answer
Kevin Korte
28,149 PointsBecause you don't have a standard post object, you have to change the the_permalink
function to get_permalink
and pass the post's ID. All you have to do is change your code to
<?php
<a href="<?php echo get_permalink($recent['ID'])">
Which by the way, is shown in the first example on the wp_get_recent_posts
function here: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts