WordPress.org

Ready to get started?Download WordPress

Forums

Reassigning post thumbnail issue (2 posts)

  1. adeizasama
    Member
    Posted 5 months ago #

    Hi,

    I'm working on a site and I'm trying to make it search through the images in the post to come up with a post thumbnail.

    The thing is, when you add the image to the post while creating it, it picks the thumbnail straight away, but when you add an image after the post has been published, it doesn't change the already assigned default image.

    Here is the code for the thumbnails:

    // Get image attachment (sizes: thumbnail, medium, full)
    function get_thumbnail($postid=0, $size='full') {
    	if ($postid<1)
    	$postid = get_the_ID();
    	$thumb_key = get_theme_mod('thumb_key');
    	if($thumb_key)
    		$thumb_key = $thumb_key;
    	else
    		$thumb_key = 'thumb';
    	$thumb = get_post_meta($postid, $thumb_key, TRUE); // Declare the custom field for the image
    	if ($thumb != null or $thumb != '') {
    		return $thumb;
    	} elseif ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => '1',
    		'post_mime_type' => 'image', ))) {
    		foreach($images as $image) {
    			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
    			return $thumbnail[0];
    		}
    	} else {
    		return get_bloginfo ( 'stylesheet_directory' ).'/images/login-logo2.jpg';
    	}
    
    }

    And here is the website -> web.ugometrics.com.

    Most of the posts with the issue have the "ugometrics" logo as the thumbnail, for your consideration.

    Any ideas?

    Thanks

  2. bcworkz
    Member
    Posted 5 months ago #

    Obviously you need script to set a new thumbnail when the post is updated. You can hook the 'publish_to_publish' action to only capture updates and not new posts. The question is what image to use if there is more than one?

    You can get the last image inserted by choosing the last element in the returned array of attachments because it is sorted by date by default. Whether that would be an appropriate thumbnail I don't know. I don't see how a script could possibly know that.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.