2

I am working on a new theme for a site, but the majority of the posts don't have a featured image set. Throughout the new theme I am pulling in featured images using the_post_thumbnail_url. I am wondering if there is a function I can add to functions.php to override this url, globally, with a url to a placeholder image if there is no featured image assigned to the post.

1

2 Answers 2

5

If you know the URL of the placeholder image, you sure can do it.

get_the_post_thumbnail_url() function calls the wp_get_attachment_image_url() function which in turns calls the wp_get_attachment_image_src() function to get the source of the post thumbnail. wp_get_attachment_url() returns the result from wp_get_attachment_image_src filter. So we can use that filter to modify the result if there is no image and the size is 'post-thumbnail'.

namespace StackExchange\WordPress;

function image_src( $image, $attachment_id, $size, $icon ) {
  $default = [
    'https://example.com/img.gif',
  ];
  return ( false === $image && 'post-thumbnail' === $size ) ? $default : $image;
}
\add_filter( 'wp_get_attachment_image_src', __NAMESPACE__ . '\image_src', 10, 4 );
4
  • Thank you, this seems like what I'm looking for, however when I add this code to my functions.php I get the error: Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script If I move it to the top it breaks a bunch of other things. Any ideas?
    – APAD1
    Commented Apr 5, 2018 at 14:24
  • 1
    If you're going to use namespaces, and you should, you should read and understand the documentation first. Otherwise, you can remove the namespace line and use a unique callback (e.g. 'wpse299844_image_src') for the filter instead. Make sure to remove the __NAMESPACE__ too. Commented Apr 5, 2018 at 14:30
  • 1
    @NathanJohnson return ... $url; or ... $image; ?
    – stckvrw
    Commented Jan 16, 2020 at 15:09
  • Once you're comfortable with this and have gotten far enough along, consider adding a theme option that allows you to quickly change out the default image without having to edit it via code. Commented Jan 17, 2020 at 14:22
2

Well, depending on the thumbnails you have defined, you could do a check if the post "has" a thumbnail, display it; but if it doesn't, you can show a default image stored somewhere in your theme's image directory, like so:

// Other code to fetch the post
if ( has_post_thumbnail() ) {
    the_post_thumbnail('slide');
}
else {
    <img src="<?php echo get_template_directory_uri() ?>/images/default-image.jpg" class="attachment-slide wp-post-image">
}

This is assuming you have already added the thumbnail slide like so:

add_image_size( 'slide', 500, 400, true ); // width=500, height=400

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.