WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have used the following code to display the featured image for each post, but nothing is showing:

     <div class="thumbnail-img">

<?php 




        $lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');

        if ($lastBlog->has_post_thumbnail()) {

            while($lastBlog->has_post_thumbnail()) {
                $lastBlog->the_post_thumbnail();
            } ?>

            <?php get_template_part('content-image', get_the_post_thumbnail()); 

        }




?>

</div>
<br>

<?php



        if( $lastBlog->have_posts()):

        while($lastBlog->have_posts()): $lastBlog->the_post(); ?> 



            <?php get_template_part('content-title', get_post_format()); ?>




        <?php endwhile;

    endif;

    wp_reset_postdata();
?>
</div>

How do I resolve this?

share|improve this question

I use this way:

$args = array ( 'post_type' => 'slider');
$slides = get_posts( $args );

foreach($slides as $row){ 
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($row->ID),"full");
    echo $image[0];
}
share|improve this answer
    
I'm not using an image slider. All I wanted it to be able to display the thumbnail over the post title. I have edited the question to show exactly what I wanted. – Michael Stokes 10 hours ago
<div class="thumbnail-img">
<?php 
    $lastBlog = new WP_Query('post_type=post&posts_per_page=2');
    if ($lastBlog->have_posts()) {
        while($lastBlog->have_posts()) {
             $lastBlog->the_post();
              if(has_post_thumbnail()){
                  the_post_thumbnail();
             }
         }
     }
  ?>
 </div>

-> Try Like This

share|improve this answer

Below code will help you to display thumbnail with size of your choice.

while($lastBlog->has_post_thumbnail()) {
/*****     Thumbnail     ******/
    the_post_thumbnail(
        array(120, 90),     //Size of the thumbnail Image
        array(

            'class' => 'class_name',     //Specify class name to thumbnail image if any
            'alt' => 'post thumbnail',   // alternate text in case if no thumbnail image to show
            'title' => 'Notice'          //Specify title if want to
        )
    );
/*******     Thumbnail Ends   ********/
    } ?>

Make any kind of formatting (according to need of project) of thumbnail image by specifying class name as shown above.

share|improve this answer

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.