How can I check for a thumbnail in WordPress?

How can I can I check if a post has a thumbnail and if does do something? If doesn’t do something else. This is what I have:

        <?php if(have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

                <?php if ( has_post_thumbnail() ) { ?>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php 
                }else{ 
                ?>
                    <?php the_post_thumbnail(); ?> 
                <?php
                } 
                ?>  

            <?php endwhile; ?>

        <?php endif; ?>

Any help will be appreciate it.

Related posts

Leave a Reply

4 comments

  1. You already have this, in the line

    if ( has_post_thumbnail() )
    

    you are checking if the post has thumbnail, the problems is that you put wrong code in else statement, you have to put something like:

      <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          <?php the_post_thumbnail(); ?> 
          HAVE THUMBNAIL DO SOMETHING
      <?php 
          }else{ 
      ?>
          DOESN'T HAVE THUMBNAIL : DO SOMETHING ELSE
          <?php
      } 
      ?>  
    
  2. Try with these line of codes:

        <?php if(has_post_thumbnail())
            { 
            ?>
                <img src="<?php the_post_thumbnail_url(); ?>" id="contextual" class="contextual" alt="" />
    
            <?php 
            }
    else{       
            ?>
            <img src="<?php echo get_template_directory_uri(); ?>/design/images/i-default.jpg" id="contextual" class="contextual" alt="" />
    <?php } ?>
    
  3. To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme’s template files:

    <?php if ( has_post_thumbnail() ) : ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <?php the_post_thumbnail(); ?>
        </a>
    <?php endif; ?>
    
  4. firstly CHECK your functions.php file for this

    if (function_exists('add_theme_support')) {
      add_theme_support('post-thumbnails');
    }
    

    if its not in there, copy and paste that into your file..

    Secondly Add this to your functions.php
    this lets you return the Image src, and not just print the entire img tag

    function get_the_post_thumbnail_url( $post_id = NULL ) {
        global $id;
        $post_id = ( NULL === $post_id ) ? $id : $post_id;
        $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
        $src = $src[0];
        return $src;
    }
    

    Then on your template page change your code to something like:
    this was used as a background image

    <?php if ( has_post_thumbnail() ) { ?>
        <div id="slider" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID, 'large'); ?>); background-position: center center;">  
        </div>                
    <?php 
    }else{ 
    ?>
        <img src="<?php bloginfo('template_directory');?>/images/blank.jpg" alt="" /> 
    <?php
    } 
    ?> 
    

    this should produce a div with a background image applied to it,

    If you want the Full img tag code to be printed simply use one of the following.

    if (has_post_thumbnail()) { 
    ?>
        <?php the_post_thumbnail();            // just the image        ?>
        <?php the_post_thumbnail('thumbnail'); // just the thumbnail    ?>
        <?php the_post_thumbnail('medium');    // just the Medium Image ?>
        <?php the_post_thumbnail('large');     // just the Medium Image ?>
        <?php 
        // adding a 200x200 height and width along with a class to it.
            the_post_thumbnail(array( 200,200 ), array( 'class' => 'alignleft' )); 
        ?>
        <?php 
        // Adding a few classes to the medium image
            the_post_thumbnail('medium', array('class' => 'alignleft another_class')); 
        ?>
    
    <?php
    }
    

    Marty..