From 68a7ef1be980cf4973a66ea98cd5bfa501fe1468 Mon Sep 17 00:00:00 2001 From: John Holt Date: Mon, 8 Dec 2014 16:14:25 -0500 Subject: [PATCH 1/3] Update related-posts-thumbnails.php Place single quotes around $url to ensure urls with spaces will be displayed properly. --- related-posts-thumbnails.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/related-posts-thumbnails.php b/related-posts-thumbnails.php index 096adf9..e82e66e 100644 --- a/related-posts-thumbnails.php +++ b/related-posts-thumbnails.php @@ -330,7 +330,7 @@ function get_thumbnails( $show_top = false ) { // Retrieve Related Posts HTML fo else { $output .= ''; $output .= '
'; - $output .= '
'; + $output .= '
'; $output .= '
' . $title . $excerpt . '
'; $output .= '
'; $output .= '
'; From d7c2d199d7bbfb1c7345d8aadac23e40c756937c Mon Sep 17 00:00:00 2001 From: John Holt Date: Mon, 8 Dec 2014 16:18:40 -0500 Subject: [PATCH 2/3] Update related-posts-thumbnails.php If $from_post_body set and no image found in post body content, then check for attached image. --- related-posts-thumbnails.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/related-posts-thumbnails.php b/related-posts-thumbnails.php index e82e66e..c25e0a9 100644 --- a/related-posts-thumbnails.php +++ b/related-posts-thumbnails.php @@ -267,7 +267,25 @@ function get_thumbnails( $show_top = false ) { // Retrieve Related Posts HTML fo preg_match_all( '||i', $post->post_content, $matches ); // searching for the first uploaded image in text if ( isset( $matches ) ) $image = $matches[1][0]; else - $debug .= 'No image was found;'; + $debug .= 'No image was found in body;'; + if (!$image) { // then check for image attachment + $p = array( + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + 'numberposts' => 1, + 'order' => 'ASC', + 'orderby' => 'menu_order ID', + 'post_status' => null, + 'post_parent' => $post->ID + ); + $attachments = get_posts($p); + if ($attachments) { + $imgsrc = wp_get_attachment_image_src($attachments[0]->ID, 'full'); + $image = $imgsrc[0]; + } + else + $debug .= 'No attachment was found;'; + } if ( strlen( trim( $image ) ) > 0 ) { $image_sizes = @getimagesize( $image ); if ( $image_sizes === false ) From 9cf5bc56bb8f966ee9acb63fa185612df4f55752 Mon Sep 17 00:00:00 2001 From: John Holt Date: Mon, 8 Dec 2014 16:26:37 -0500 Subject: [PATCH 3/3] Update related-posts-thumbnails.php Create the thumb if it doesn't exist. --- related-posts-thumbnails.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/related-posts-thumbnails.php b/related-posts-thumbnails.php index c25e0a9..f22cab1 100644 --- a/related-posts-thumbnails.php +++ b/related-posts-thumbnails.php @@ -298,6 +298,21 @@ function get_thumbnails( $show_top = false ) { // Retrieve Related Posts HTML fo $debug .= 'Changing image according to Wordpress standards;'; $url = preg_replace( '/(-[0-9]+x[0-9]+)?(\.[^\.]*)$/', '-' . $width . 'x' . $height . '$2', $image ); } + // create thumb from image if it doesn't exist + $u = wp_upload_dir(); + $wpcurl = strlen($u['baseurl']); + $source = $u['basedir'].substr($image, $wpcurl); + // if source is a WP resize, grab the full original source instead + $source = preg_replace( '/(-[0-9]+x[0-9]+)?(\.[^\.]*)$/', '$2', $source); + $thumb = $u['basedir'].substr($url, $wpcurl); + if (!file_exists($thumb) && file_exists($source)) { + $debug .= 'Creating new thumbnail: ' . $thumb . ' from source: ' . $source . '; '; + require_once(ABSPATH.'wp-admin/includes/image.php'); + $img = image_resize($source, $width, $height, true); + $debug .= 'image_resize returned ' . $img . '; '; + // NOTE! WP image_resize function automatically forces lowercase .jpg extension, even if source has uppercase .JPG + $url = preg_replace('/.JPG/','.jpg',$url); // make sure url is lowercase .jpg to match + } } else $debug .= 'Found wrong formatted image: '.$image.';';