Lewman
Lewman

Reputation: 39

Thumbnail for posts duplicates on front page

The featured thumbnail image is supposed to just show the single thumbnail image of the post but instead it pulls every post image and displays them instead for every post. Instead of just the post thumbnail. This is the code for it and also what it looks like on the page:

<?php $featured_image = new WP_Query('page_id=ID'); ?>
<?php while ($featured_image->have_posts()) : $featured_image->the_post(); ?>


<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>

    <?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(302, 170)); ?>

    <div class="the-image">
        <a href="<?php echo get_permalink($p['ID']) ?>"><img src="<?php echo $img_src[0]; ?>" /></a>
    </div>

<?php }; ?>

<?php endwhile; ?>

thats the code for servering the image and heres what happens on the main page (I have 2 posts currently and a featured test image for both of them, the scaling works but as you can see, it puts both images, on both posts...)

enter image description here

I'm not sure if it needs to be inside my loop for the title and excerpt, or outside of it (currently inside).

Here is the full code for the recent posts sidebar:

 <div class="col-lg-4 d-none d-lg-block">
 <h3 style="text-align: center; font-weight: 700;">Recent Posts</h3>
 <?php

 $result = wp_get_recent_posts(array(
 'numberposts' => 10,
 'category' => '',
 'post_status' => 'publish',
 ));


 foreach( $result as $p ){
 ?>

  <div class="paddingarea text-dark">

  <?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>

   <?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(302, 170)); ?>

       <div class="the-image">
       <a href="<?php echo get_permalink($p['ID']) ?>"><img src="<?php echo $img_src[0]; ?>" /></a>
       </div>

   <?php }; ?>

  <a class="card-title" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600;"><?php echo $p['post_title']?></a><br />
   <p class="card-text"><?php echo excerpt(10); ?></p>
  </div>
  <?php
 }
?>

  </div>

Upvotes: 0

Views: 42

Answers (1)

kaize
kaize

Reputation: 811

Try this, it's simple and it's working for me:

<?php

 $result = wp_get_recent_posts(array(
 'numberposts' => 10,
 'category' => '',
 'post_status' => 'publish',
 ));

 foreach( $result as $p ){
 ?>

  <div class="paddingarea text-dark">

    <div class="the-image">
        <a href="<?php echo get_permalink($p['ID']) ?>"><img src="<?php echo get_the_post_thumbnail_url($p['ID'], array(302, 170)); ?>" /></a>
    </div>

  <a class="card-title" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600;"><?php echo $p['post_title']?></a><br />
   <p class="card-text"><?php //echo excerpt(10); ?></p>
  </div>
  <?php
 }
?>

Upvotes: 1

Related Questions