Support » Developing with WordPress » Post thumbnail fallback?

  • Resolved oguruma

    (@oguruma)


    I am making a single.php template using get_the_post_thumbnail_url(); inside the loop.

    This is to make a “hero” header for the post, and the post_thumbnail is used as the backfround for the header.

    I want to create a fallback, in the event that there is no featured image.

    Is there a way to set a fallback for this function?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Howdy!

    One way I can think of would be using the has_post_thumbnail() function ( https://developer.wordpress.org/reference/functions/has_post_thumbnail/ ) and if that fails use what ever you’d like after that.

    Thread Starter oguruma

    (@oguruma)

    @jcastaneda Thanks for the input!

    It worked flawlessly

    I used the function below. The function will echo the Featured Image URL if it has one, if not it, uses an image I put in a folder in a subdirectory in the theme directory.

     <?php
    // Must be inside a loop.
     
    if ( has_post_thumbnail() ) {
       echo get_the_post_thumbnail_url();
    }
    else {
        echo get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg';
    }
    ?>  
    • This reply was modified 1 year ago by oguruma.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Happy to hear that. Don’t forget to mark as resolved just in case 😉

    Thread Starter oguruma

    (@oguruma)

    Resolved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post thumbnail fallback?’ is closed to new replies.