WordPress.org

Forums

[resolved] Display Featured Image with latest post grab (4 posts)

  1. Action Turtle
    Member
    Posted 1 year ago #

    Hey everyone… I am at a loss :( I got this far but I am only able to pull a static image with the post to display on my home page… I would like to dynamically display the featured image for the post… Here is the code I have so far

    <?php $the_query = new WP_Query( 'showposts=3' ); ?>
      <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
      <ul class="one_up tiles">
                  <!-- Get the single service -->
                  <li class="service">
    
                   <div class="two columns">
                   <img src="<?php echo get_template_directory_uri(); ?>/images/blogpost.png" alt="blog-post" /> 
    
                   </div>
                   <div class="nine columns">
                     <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                     <p><?php echo string_limit_words(get_the_excerpt(), 35); ?></p>
                 </div>
                  </li>
          </ul>

    I am pretty sure I need to put new code where it says img src=

    Does anyone know what code I need to dynamically grab the featured image for the post that I am pulling?

    Like for the words I was able to put (get_the_excerpt(), 35) to make sure I was not pulling the whole post…

    Thank you so much for any help…

  2. SpankMarvin
    Member
    Posted 1 year ago #

    You'll want to replace the img tag with

    <?php the_post_thumbnail(); ?>

  3. Action Turtle
    Member
    Posted 1 year ago #

    Thanks so much for your help Marvin…

    It seems to be working but for some reason I am getting the missing image display rather than the image that I have set to the featured image…

    I checked that I have thumbnail support enabled in my functions.php file

    I double check that images were definitely uploaded to the server and wordpress properly broke down the images into the proper resolutions…

    So I really don't understand why I am getting the missing image tag… Thanks man

  4. Action Turtle
    Member
    Posted 1 year ago #

    Hey figured it out :)

    I was doing…

    <img src="<?php the_post_thumbnail(); ?>" />

    I needed to be doing…

    <?php the_post_thumbnail(); ?>

    Funny that you actually need less code as wordpress get more advanced… Cool… Thanks…

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.