Display Post Thumbnail Without Being Featured Image

I’m hoping that I can explain what I’m going for here.
On my current project’s main page, I’m displaying each post’s Featured Image Thumb, along with an excerpt of the post’s content.
In each of the actual post pages, there is a default WP gallery with 2 to 4 images inside.

What I want to do, is make it so that my client doesn’t always have to use a Featured Image in each post, so that the thumbnail always shows up on the home page. In other words, right now, he has to pick an image to be Featured in order for there to be that representative thumbnail for each post in the loop on the home page.

Can I make it so that even if he doesn’t pick a featured image for a post, there still is a thumbnail to represent the post? Can I make it automatically just pick the first image in the post’s gallery, if no featured image is chosen?

Just in case, here is some of what I’m using, code wise:

              <div id="image-wrap">
             <?php 
              if ( has_post_thumbnail() ) {
              the_post_thumbnail();
               } 
              ?>




         </div><!--end image-wrap-->


                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                 <?php the_title('<h2 class="entry-title"><a href="' .  

get_permalink() . '" title="' . the_title_attribute('echo=0') . '" 
rel="bookmark">', '</a></h2>'); ?>

                 <div class="entry-content">
<?php the_content(__('Continue reading', 'example')); ?>
<?php wp_link_pages('before=<p class="pages">' . __('Pages:','example') .

'&after=</p>'); ?>
</div>





</div>

<?php endwhile; ?>

<?php else : ?>

<p class="no-posts"><?php _e('Sorry, no posts matched your criteria',  
'example'); ?></p>

<?php endif; ?>
<?php wp_reset_query(); ?>

Then this is in my FUNCTIONS File:

 // This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 100, true );




// Automatically makes featured image thumbs a clickable link
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id ) {

$html = '<a href="' . get_permalink( $post_id ) . '" rel="nofollow noreferrer noopener" title="' . esc_attr(   
get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
return $html;

}

// This theme displays full size featured image on the Post's page
function InsertFeaturedImage($content) {

global $post;

$original_content = $content;

if ( current_theme_supports( 'post-thumbnails' ) ) {

    if ((is_page()) || (is_single())) {
        $content = the_post_thumbnail('page-single');
        $content .= $original_content;

    }

}
return $content;
}

add_filter( 'the_content', 'InsertFeaturedImage' );

http://dependablecarcompany.com is the address if you care to see what I’m talking about. You’ll see what I mean when looking at the post titled: “1991 GMC Sierra”. I did not use a featured image for the post, so there is no thumbnail shown. Thanks in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Can’t recommend Get the Image plugin (by Justin Tadlock) enough. It’s among the best out there, very well maintained and neat code — more importantly, it does what you need, while giving you a whole slew of options.

How does it pull images?

  1. Looks for an image by custom field (one of your choosing).
  2. If no image is added by custom field, check for an image using the_post_thumbnail() (WP 2.9’s new image feature).
  3. If no image is found, it grabs an image attached to your post.
  4. If no image is attached, it can extract an image from your post content (off by default).
  5. If no image is found at this point, it will default to an image you set (not set by default).

Method 2

Just check for the thumbnail, and if it’s not set, use the first image from the gallery instead. Something like this:

$size = 'thumbnail'; // whatever size you want
if ( has_post_thumbnail() ) {
    the_post_thumbnail( $size );
} else {
    $attachments = get_children( array(
        'post_parent' => get_the_ID(),
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'numberposts' => 1)
    );
    foreach ( $attachments as $thumb_id => $attachment ) {
        echo wp_get_attachment_image($thumb_id, $size);
    }
}

Basically, if no featured image exists, then has_post_thumbnail() will return false. So you call get_children to get the attached images for this post. Note numberposts = 1, so it only gets the first one. Then you output that image using wp_get_attachment_image.

Note that I used the foreach even though I’m only getting 1 image here. This is because get_children returns an array of posts, regardless of how many posts it’s returning. so I’m “looping” through an array of size 1. If there’s no images, the array will be empty and so nothing is output.

If you don’t like using get_children, then a similar new WP_Query could be constructed to get the first attachment image in a similar manner.

  • Added the missing curly brace to the foreach loop.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x