automatic thumbnail using functions
Showing automatic thumnails still great to discuss. In my previous post, i have posted about Get The Image plugins that could help us to show automatic thumbnail, in this post i want to show you how to modify our theme, so our theme will show automatic thumbnail without plugin’s aid at all. The first idea were came after i read this post, and i have try it. Success!!
First step that we should modify is the functions file. (maybe funtions.php)
add this script below
- function get_image() {
- global $post, $posts;
- $first_img = '';
- ob_start();
- ob_end_clean();
- $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
- $first_img = $matches [1] [0];
-
- if(empty($first_img)){ //Defines a default image
- $first_img = "/images/default.jpg";
- }
- return $first_img;
- }
After that script above already inserted into your Functions file, all you need to do to call the thumbnail is with this script :
- <?php echo get_image(); ?>
That’s script (above) result is showing the image url, so if you want to make the image appear, you must use call it with “scr” script. The example is like this script below :
- <img src="<?php echo get_image(); ?>" />
That’s it. With this modifications, you can show automatic thumbnail from the first image that inside your post. Just in case there is no image inside your post, then the $first_img = “/images/default.jpg”; will works.
You can change it with your own default image. Example : $first_img = “http://www.yourdomain.com/default.jpg”;
Have a nice try, if you have any trouble with this script, leave a comments.




Hi,
you also know a CSS code to auto resize youtube fragments ?
I created this one for images :
/* Max image width */p img { max-width: 600px; height: auto;}
[Reply]
[...] Add new code before “add_filter(” with this script below (Insert below number 2, and before number 3). You can read more detail about this Functions Script here [...]
[...] Change that functions above with this script : Source code function get_image_url(){global $post, $posts;$first_img = '';ob_start();ob_end_clean();$output = preg_match_all('//i', $post->post_content, $matches);$first_img = $matches [1] [0];echo $first_img;if(empty($first_img)){ //Defines a default image$first_img = "/images/default.jpg";}return $first_img; } This script were same like my previous post here : Automatic Thumbnail using functions [...]