Oliver Prenzel
Oliver Prenzel

Reputation: 31

Check for Wordpress Thumbnail and display dummy if not there

got another question to ask:

I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.

Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):

<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="gallery_image_container">
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <div class="gallery_image_thumb">
                <?php if ( has_post_thumbnail() ) { 
                    the_post_thumbnail('thumbnail'); }
                else {
                    echo 
                    '<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
                ; } ?>

            </div>
            <div class="gallery_title">
                <h2>
                    <?php the_title(); ?>
                </h2>
            </div>
        </a>
    </div>
<?php endwhile; else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> 

What am I doing wrong here?

Upvotes: 0

Views: 3652

Answers (1)

Pagerange
Pagerange

Reputation: 234

This code works for me. Yours is virtually identical.

<?php if ( has_post_thumbnail() ) : ?>

        <?php the_post_thumbnail(); ?>

    <?php else : ?>

  <img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />

    <?php endif; ?>

Note: I also cut and pasted your code and tested on my WP install, and it worked fine.

Or... try this alternative to the if condition:

<?php if ( get_the_post_thumbnail( get_the_ID() ) ) { 
                    the_post_thumbnail('thumbnail'); }
                else {
                    echo 
                    '<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
                ; } ?>

Upvotes: 2

Related Questions