• Resolved lschoen114

    (@lschoen114)


    Hello,

    I have the following code in my single.php file, which grabs the featured image and displays is as a background image for that post.

    <figure class="thumblucas"><?php the_post_thumbnail('slider-post-thumbnail'); ?></figure>

    I am trying to create an IF function that say, If there is a featured image, display that image using the code above. If there is not one, display a generic chosen image. I have found the following code which would work, but its not working with what I am trying to do.

    <?php
    // Must be inside a loop.
    
    if ( has_post_thumbnail() ) {
    	<figure class="thumblucas"><?php the_post_thumbnail('slider-post-thumbnail'); ?></figure>;
    }
    else {
    	echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
    }
    ?>

    Is there any way to accomplish something like this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • but its not working with what I am trying to do

    what is the result?
    do you have the thumbnail-default.jpg image located in the /images folder of your theme?

    Thread Starter lschoen114

    (@lschoen114)

    The result is that is breaks the entire page.

    For some reason the if code “<figure class=”thumblucas”><?php the_post_thumbnail(‘slider-post-thumbnail’); ?></figure>” is not accepted when I try and do it.

    All that shows up is a white page.

    you have missing php tags;

    try:

    <?php
    // Must be inside a loop.
    
    if ( has_post_thumbnail() ) { ?>
    	<figure class="thumblucas"><?php the_post_thumbnail('slider-post-thumbnail'); ?></figure>
    <?php }
    else {
    	echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
    }
    ?>
    Thread Starter lschoen114

    (@lschoen114)

    That worked!

    Thank you for spotting that for me!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘the_post_thumbnail IF ELSE function issue’ is closed to new replies.