Post Thumbnails without custom field

I encounter situations where i need to automatically display a thumbnail or image of Books that will link to posts. Now i found the solution to display Thumbnails without custom field.  Here’s an example of this kind of situation:

Write the given code in functions.php

function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, ‘<img’);
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, ‘<img’, $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, ‘>’);
$postOutput = substr($post, 0, $imgEnd+1);
$result = preg_match(‘/width=”([0-9]*)” height=”([0-9]*)”/’, $postOutput, $matches);
if ($result) {
$pagestring = $matches[0];
$image[$i] = str_replace($pagestring, “”, $postOutput);
} else {
$image[$i] = $postOutput;
}
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img’)) { echo ‘<a href=”‘.$link.'”>’.$image[$num].”</a>”; }
$more = 0;
}

Code for calling thumb img

<?php getImage(‘1’); ?>

(“1” 1st image in the post…)

Advertisements

One Response to “Post Thumbnails without custom field”

  1. Ryan Says:

    nice post, really appreciated

    also added a post a WordPress post tweak

    “Automatic WordPress Thumbnail Without Custom field and Featured image” yes it is automatic


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: