How to show post thumbnail in wordpress?

3 replies
I am using wordpress and I developed my own theme for wordpress. Now I would like to show the thumbnail of one of the images uploaded for the post. I used
PHP Code:
<?php echo get_the_post_thumbnail($post->ID'thumbnail'); ?>
to fetch that but thrown error by the compiler. How do I get the img src for the thumbnails in wordpress?
#post #show #thumbnail #wordpress
  • Profile picture of the author SteveJohnson
    First, make sure post thumbnails are enabled in your theme. In the theme functions.php file add this:
    Code:
    if ( function_exists( 'add_theme_support') )
      add_theme_support('post-thumbnails');
    Then in your templates you can call the image in several ways. The easiest:
    Code:
    if ( has_post_thumbnail() )
      the_post_thumbnail();
    The above will output an img tag with the proper src address.

    The main Codex page for post thumbnails: Post Thumbnails « WordPress Codex
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[5315297].message }}
    • Profile picture of the author mpchekuri
      @SteveJohnson Thanks for you help.
      This works well when one of the images is selected as thumbnail. What if no image is selected as thumbnail and want to fetch the thumbnail of first image in the post.
      {{ DiscussionBoard.errors[5315326].message }}
      • Profile picture of the author SteveJohnson
        Originally Posted by mpchekuri View Post

        @SteveJohnson Thanks for you help.
        This works well when one of the images is selected as thumbnail. What if no image is selected as thumbnail and want to fetch the thumbnail of first image in the post.
        That's a little more difficult.

        First you have to get the attachments for the post (this can only be done inside the WordPress Loop where you have access to the $post object):
        PHP Code:
        <?php args = array( 'post_type' => 'attachment''numberposts' => -1'post_status' => null'post_parent' => $ post->ID );
        attachments get_posts($ args);
        if ($ 
        attachments) {
          foreach ( $ 
        attachments as $ attachment ) {
            echo 
        apply_filters'the_title' , $ attachment->post_title );
            
        the_attachment_link( $ attachment->ID false );
          }
        }
        ?>
        You'll have to remove the spaces after the $ signs in the code as this forum's text editor removes variable names completely.

        Anyway, inside the foreach statement above, each $ attachment is an object containing the data for each attachment in the post. You'll need to do a print_r() of $ attachment to see what properties are available.

        Since $ attachments is an array of attachments, you can use $ attachments[0] to get the first image attached to the post rather than looping through the array.
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[5316127].message }}

Trending Topics