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.
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
}
?>
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 } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With