Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
jorame Avatar asked Feb 16 '12 03:02

jorame


2 Answers

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
  } 
  ?>  
like image 171
eveevans Avatar answered Oct 03 '22 05:10

eveevans


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 } ?>
like image 28
shakil Avatar answered Oct 03 '22 06:10

shakil