Catch the First Image (Upgraded)

Brian asked me here about Catch the First image function can be upgrade. I was coded this function for my Turkish blog for Home Page thumbs. If you set no image about post (not the wordpress post thumb feature, if you dont upload any image) this function get custom (default) image for this. Let’s Begin…

The Code;

You will edit your theme’s functions.php. Because of this you should backup before. File’s path is /wp-content/themes/your-theme/functions.php

// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
	if ($postid<1)
	$postid = get_the_ID();
	$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
	if ($thumb != null or $thumb != '') {
		echo $thumb;
	}
	elseif ($images = get_children(array( //If you upload an image function gets first image
		'post_parent' => $postid,
		'post_type' => 'attachment',
		'numberposts' => '1',
                'orderby' => 'menu_order',
                'order' => 'ASC', //If reverse use DESC
		'post_mime_type' => 'image', )))
		foreach($images as $image) {
			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
			?>
	<?php echo $thumbnail[0]; ?>
	<?php }
		else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
		echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
		echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
	}
}

You can use this function with your several files like index.php, single.php or anything.

<img src="<?php sa_get_image($post->ID, 'thumbnail'); ?>" />

If you set your thumbnail size 100*100 from wordpress you can get a thumbnail image where you want. Also you can use another function with that. You can style your image’s div or you can use timthumb.

Without Timthumb;

// Show Post Thumbnails
function sa_show_thumb_tim() {
?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img class="thumb" width="100" height"100" src="<?php sa_get_image($post->ID, 'full'); ?>" alt="<?php the_title(); ?>" /></a>
<?php
}

You can use this like this directly. Because we coded function with img tag.

<?php sa_show_thumb(); ?>

With Timthumb;

// Show Post Thumbnails With TimThumb
function sa_show_thumb_tim() {
?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img class="thumb" src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php sa_get_image($post->ID, 'full'); ?>&amp;h=100&amp;w=100&amp;zc=1" alt="<?php the_title(); ?>" /></a>
<?php
}

You can use this like this directly. Because we coded function with img tag and we add timthumb function. You can get the latest version of timhtumb here. And you have to put timthumb.php into your theme’s folder.

<?php sa_show_thumb_tim(); ?>

Best Regards


Related posts:

If you enjoyed this post, make sure you subscribe to my RSS Feed
  • http://yoursn0w.com Ali Wadi

    Thanks this code works perfectly!

    I have two question.
    Where can I resize the generated thumbnail? 

    And How to make it grab the first image like your previous function you had?

    • http://www.kaisercrazy.com Serkan Algur (kaisercrazy)

      Hello Ali,

      Thanks for your reply. 
      First Solution. You can use Renegerate Thumbnail Plugin for wordpress. Set the thumbnail size from WordPress Dashboard Media Settings and use this plugin. You can download from wordpress.org http://wordpress.org/extend/plugins/regenerate-thumbnails/

      And second solution :) 
      Look code :) You can see echo $thumbnail[0]; :))

      • http://yoursn0w.com Ali Wadi

        Thanks for your quick response,

        The problem is that the first image it grab it was the first from the array, but your previous function was the first image from the post content.

        So when I change numberposts => ‘-1′ I can see list of URL image with lots of space delimiter and the problem is that the first image store as last.

        And I cant just simply change the index from $thumbnail[0] to any other number it will just give me wrong output like http://mydain.com

        • http://www.kaisercrazy.com Serkan Algur (kaisercrazy)

          Ali,

          I think we have a simple soluton :) Can you add this in the array area;

          ‘orderby’ => ‘menu_order’,
          ‘order’ => ‘ASC’, //If reverse use DESC

          Also i don’t know any solution about space problem in the link. If i find the solution i will inform you :)

          • http://yoursn0w.com Ali Wadi

            @kaisercrazy:disqus  This is awesome thank you so much now I have that image display in the right order. Only left to find about the image dimension 100×100

            Thanks!

          • http://www.kaisercrazy.com Serkan Algur (kaisercrazy)

            Ali,

            Go to your dashboard, Settings->Media, set the Thumbnail Size to 100×100 (default 150×150). And use Regenerate Thumbnail Plugin :)) You can use 100×100 thumbnails after that.

Highslide for Wordpress Plugin