0

I'm trying to create a grid-type view for a custom home page. it will be a simple grid that will contain three columns, with several previous posts in each column. I'd like for each instance to contain:

  • Linked Title linked
  • featured image

The problem that I'm running in to is that the same featured image is being displayed for each post. The post titles are all correct - I can see each individual, and different, post title.

How can I get this bit of code to get the featured image for that particular post instead of repeating the same post over and over again? Not great with PHP so I'm fairly sure the problem is in that part, just don't know what I need to change.

I also want to add a short summary and a post link, but didn't want to do that until I got this issue solved first.

Here is what I have so far in my front-page.php file:

<?php 
$args = array( 'numberposts' => '99' );
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
    echo '<div class="medium-4 small-12 columns">'; 
    echo  '<div class="fp-featured-image"> ';
    echo get_the_post_thumbnail($post_id, 'featured-image');

    echo '</div>';
    echo '<h2 class="my-post-title"><a href="' . get_permalink($recent["ID"]) . '">' .   ( __($recent["post_title"])).'</a></h2> </div> ';
}
?>
0

Do not use $post_id. From this answer by @s_ha_dum (which you should go and read).

$post_id is a variable name used commonly to refer the post ID, but it isn't a Core variable the way that $post is.

In the context as in your question, you would want to use $recent["ID"] (which will hold the current post id in the foreach loop) instead of $post_id which will hold the page ID of the page the query is being run on

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.