• Hi!

    I have an issue with a piece of code I’d like to use to be able to show a default image whenever a thumbnail is not defined:

    <?php
    	if ( has_post_thumbnail() )
    		the_post_thumbnail( 'single-post-thumbnail' );
    	else
    		echo '<img src="default_thumb.jpg" alt="title" title="title" />';
    ?>

    Problem is, this code snippet returns no image. I tried using <?php bloginfo('template_directory'); ?> in front of my image but it causes an error. There must be something I am doing wrong here, but I don’t know what.

    Anybody could help?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try it like this:

    <?php
    	if ( has_post_thumbnail() )
    		the_post_thumbnail( 'single-post-thumbnail' );
    	else
    		echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/default_thumb.jpg" alt="title" title="title" />';
    ?>

    you might need to adjust the url to “default_thumb”. The example assumes that it is in the same directory as your themes main stylesheet.

    Thread Starter Jeremy Herve

    (@hd-j)

    That works perfectly. I should really learn some PHP properly…

    Thank you!

    No problem… whenever you’re doing something like this just remember that get_bloginfo() returns a value while bloginfo() prints a value. They both accept exactly the same arguments.

    Hi there… This is exactly what I am looking for… We update the site via email from a cell phone and don’t always have a photo to send… If anyone has time, I would love some help trying to adapt this to my site… I am savvy enough to implement certain things, but woefully ignorant in regards to this… I appreciate any help in advance…

    Thank you,

    Tracy Lee

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘the_post_thumbnail replaced by default image’ is closed to new replies.