1

I have hit a wall. I have archive pages with some posts that have featured images which display fine. Some posts don't have featured images but the first attached image displays. Some posts have no images and there is a blank space. I have been trying all day to find a way to display a fallback image for the posts with no images. The closest I have come is a filter that displays a default image if no featured image is set. That would be great but it overrides the posts that normally display the first attached image. My last attempt was to only display the default image if there was no featured image or attached image. It still overrides attached images. Any ideas will be greatly appreciated. This is the last filter I have tried.

add_filter( 'genesis_pre_get_image', 'default_featured_image_when_not_set', 10, 3          );
function default_featured_image_when_not_set( $post ) {
if ( '' != get_the_post_thumbnail() || ( '' != $args['fallback']) ) {
return $post;
}
else
{
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/default-image.jpg" />';
}
}

closed as off-topic by Howdy_McGee, Robert hue, TheDeadMedic, EAMann Aug 23 '15 at 13:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

You could use get_attached_media() function to check if the post has any attached images to it. Also, you use wrong logic operator. Your code would look like this:

$attached_images = get_attached_media('image', $post->ID);
if ( (get_the_post_thumbnail() == NULL) && empty($attached_images) ) {
 // show your default image
}
else
{
 // do nothing, I guess?
}

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