Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
function grab_that_image() { ###################################### // by Paul Burgess http://iampaulburgess.co.uk // based on the original catch_that_image function found here: // http://wordpress.org/support/topic/246893#post-1212902 // for this to work you need Tim Thumb already up and running: // http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/ // and a custom field in your WP site called 'first_img'- which you set to say 'none' if you don't want an image to show // also upload a default image to use as a fall back to /wp-content/uploads/default.jpg ###################################### global $post, $posts; //absolute path to timthumb file - with NO trailing slash $path_to_thumb_file = "/wp-content/uploads"; $thumb_file_name = "timthumb.php"; // complete path $default_image = "/wp-content/uploads/default.jpg"; $first_img = ''; ob_start(); ob_end_clean(); // custom field for first image $first_img = get_post_meta($post->ID, "first_img", $single = true); // if custom field is empty... if(empty($first_img)){ // we'll try for the first image in post $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img_found = $matches [1] [0]; /* To allow Tim Thumb to work: - Remove domain - and Tim Thumb file path in case its already in use */ $first_img = str_replace(array("http://".$_SERVER['SERVER_NAME']."","".$path_to_thumb_file."/".$thumb_file_name."?src="),'',$first_img_found); // If the image is hosted on another website, we can't auto generate the thumbnail - so we'll fall back to the default // if $orig_img contains http:// and not the site server name - it's an external image if(stristr($first_img_found,"http://") && !stristr($first_img_found,$_SERVER['SERVER_NAME'])){ // therefore $first_img is the default $first_img = $default_image; } // end if for checking if image is external } // end if for checking if there's no custom field // Include the option to have no image display via the custom field // this goes back to the custom field - if it says 'none' then we won't show an image if ($first_img == "none" || empty($first_img_found)){ $image_html = NULL; } else { $image_html = "<img src=\"".$path_to_thumb_file."/".$thumb_file_name."?src=$first_img&w=210&h=130&zc=1&q=90\" alt=\"".get_the_title($post->post_parent)."\" />"; } return $image_html; } ?>
This paste will be private.
From the Design Piracy series on my blog: