Support » Plugin: Display Posts Shortcode » Show First Image if no Featured Image

  • Resolved Alkorr


    Hi, this plugin seems to be doing exactly what I’m looking for except for one thing. As many other sites, I don’t use Featured Images and I would like to show the first image from a post instead. Is it possible?

    Thanks a lot! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Bill Erickson


    While possible, you’ll need to write custom code to do this. You can use the display_posts_shortcode_output filter to modify the output of the plugin. Check the $original_atts to see if image_size is defined (ie: this shortcode should include an image), and if $image is empty, grab first image.

    It’s up to you to figure out the best way to select the first image. I know Genesis does this by querying for all ‘attachment’ posts with the current post’s ID set as page_parent. This returns a list of all images uploaded directly to this post, but wouldn’t include images uploaded elsewhere and simply inserted in the post content.

    Alternatively, you could go through the post content itself and try to find the first img tag.

    I am trying to accomplish something similar, but I want to include a default fallback image. I’ve tried adding the following to my functions.php and can’t get it to work. Any suggestions?

    If I add the else conditional directly to the plugin file, it works perfectly, but I can’t figure out how to accomplish it without modifying the original plugin.

    function add_default_image_thumbnail( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class ) {
            if ( $image_size && has_post_thumbnail() && $include_link ) {
    			$image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> ';
    		} elseif( $image_size && has_post_thumbnail() ) {
    			$image = '<span class="image">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</span> ';
    		} elseif( $image_size ) {
                $image = '<a class="image" href="' . get_permalink() . '">' . '<img width="740" height="400" src="" class="attachment-home-top size-home-top wp-post-image" alt="">' . '</a> ';
      $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $excerpt . $content . '</' . $inner_wrapper . '>';
      return $output;
    add_filter( 'display_posts_shortcode_output', 'add_default_image_thumbnail', 10, 9 );
    • This reply was modified 1 year, 1 month ago by  kevinmcullen.
    Plugin Author Bill Erickson


    Add that code to your theme’s functions.php file, or a Core Functionality plugin.

    If it’s not working when in your functions.php file, maybe there is an error in your theme file. I recommend enabling debug mode which will display the errors and help you diagnose the issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show First Image if no Featured Image’ is closed to new replies.