WordPress.org

Forums

[resolved] the_post_thumbnail replaced by default image (5 posts)

  1. Jeremy Herve
    Member
    Posted 5 years ago #

    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!

  2. Michael Fields
    Themer
    Posted 5 years ago #

    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.

  3. Jeremy Herve
    Member
    Posted 5 years ago #

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

    Thank you!

  4. Michael Fields
    Themer
    Posted 5 years ago #

    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.

  5. mstracylee
    Member
    Posted 5 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic