Mohd Yusuf
Member
Posted 1 year ago #
Hi, i am wondering if it would be easy to auto take first image in the post as the featured image in latest WordPress 3.0.
I searched for a plugin or guides; but i couldnt find anything useful for now.
Anyone has some ideas on this? Some reference if i were to mod it myself (i know php, i just dont have wordpress function database in my head database :P )
Thanks..
Mohd Yusuf
Member
Posted 1 year ago #
I ended up with my own function that i wrote to get the first image of the post and create a thumbnail on the fly.
function catch_that_image() {
global $post, $posts;
$first_img = '';
$new_img_tag = "";
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image with 0 width
$new_img_tag = "<img src='./images/bargainsfrenzy.com.gif' class='alignleft' />";
}
else{
echo $new_img_tag = "<img src='./thumb.php?gd=2&src=".$first_img."&maxw=120' class='alignleft' />";
}
return $new_img_tag;
}
Then i hacked the WPTouch theme (i added the catch_that_image() into functions.php) and I hacked the WPTouch's index.php to call catch_that_image() instead.
It works for me for now; I am still open for the 'featured image' solution though..
I have been looking high and low for a plugin or something that can do this. Anyone have some ideas?
Mohd Yusuf
Member
Posted 1 year ago #
@mkearl, so far i havent seen one. Not sure i have time to make it a proper plugin. Maybe someone could improve the code into a plugin?
Hey, give this a try (just found it). Seems to work pretty good for me right now.
http://www.sanisoft.com/blog/2010/04/19/wordpress-plugin-automatic-post-thumbnail/
--Matt
Mohd Yusuf
Member
Posted 1 year ago #
Sounds good. Will give it a try later.
code in second post will cause lags, php doesnt like regexp, especially processed on big text content
istead you should use http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
to get id of attachment you can use function get_children
http://codex.wordpress.org/Function_Reference/get_children
Mohd Yusuf
Member
Posted 1 year ago #
Thanks kkarpieszuk. That is very helpful.
kannan921
Member
Posted 1 year ago #
Thanks mkearl. Works Perfect with Pegasus theme.
Mohd Yusuf
Member
Posted 1 year ago #
I tried http://wordpress.org/extend/plugins/auto-post-thumbnail/ but I wonder if I have to regenerate the thumbnail everytime i have a new post?