Support » Fixing WordPress » Help: Set Featured Image

  • Resolved Beer

    (@beer)


    I’m using the code below in my theme:
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail('homepage-thumb'); } ?>

    The issue is I’ve just made this theme update, but it only works if in the post itself it has a featured image set. I’d like to show a ‘homepage-thumb’ from the post, even if the featured image hasn’t explicitly been set.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Everything you need to know about WordPress 2.9’s post image feature

    Toward the bottom of that writeup is a plugin for displaying other images as featured image

    thanks Rev. I’ve modified my code to this so far….

    <?php
    if ( has_post_thumbnail() ) {
    	the_post_thumbnail('homepage-thumb');
    } else {
    	// get the ID for an attachment for this post
    	$thumb = $wpdb->get_row("SELECT ID, post_title
    		FROM {$wpdb->posts}
    		WHERE post_parent = {$post->ID}
    		AND post_mime_type LIKE 'image%'
    		ORDER BY menu_order LIMIT 1");
    
    	if (! empty($thumb)) {
    		echo wp_get_attachment_image($thumb->ID, 'homepage-thumb');
    	}
    }
    ?>

    After reading the codex, this might be better.

    if ( has_post_thumbnail() ) {
    	the_post_thumbnail('homepage-thumb');
    } else {
    	$args = array( 'post_type' => 'attachment', 'post_parent' => $post->ID, 'order_by' => 'menu_order', 'order' => 'ASC' );
    	$attachments = get_posts($args);
    	if ($attachments) {
    		// display just the first attachment result
    		echo wp_get_attachment_image($attachments[0]->ID, 'homepage-thumb');
    	}
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Help: Set Featured Image’ is closed to new replies.