Most of my story-type nodes have an image embedded within the node body -- I'm using tinyMCE for formatting, however I don't think this matters as far as this little hack goes. Essentially, I'm using code within my node template to check the node body for an image; if it finds one, it is displayed within the teaser. It's just a way to add a graphical element to the node teasers. The only time this doesn't work is if there is more than one image within the node; I don't know why this is.

Add this code to your node.tpl.php file:

<?php // find an image to display
if ($page == 0) {

$find= "/src=\"(.+?)\"/i";
$text2search= $pages[0];
preg_match($find, $text2search, $matches);
if ($matches[1]) {
	$imgttl = check_plain($title);
	$found_image = "<a href='$node_url'><img src='".$matches[1]."' style='float: left; padding: 5px;' alt='$imgttl' title='$imgttl'></a>";
}
else { $found_image = ""; }
}
?>

Alternatively, you can have a default $found_image that can be used if no image is found in the node. Just put the details in the else statement above.

Next, put the following code just before your $content variable

<?php if ($page == 0 && $found_image) { print $found_image; } ?>

At this point, the full-sized image will be displayed. In order to make it smaller, you can try just adding a width or height setting to the image tag, or use CSS much like I've done in this example.

Note that there are other options to get some sort of teaser image going, but these involve installing other modules. I was just looking for a simple way to accomplish the same thing.

Works in FF2.0, Opera 9.21 and IE6 -- although IE tries its best, it still gets a little confused and may not get the image proportion exactly correct.

Brad

Comments

killerwhale65’s picture

Hi,

I have been trying this, but it doesnt work. But i do not understand the code, your search string, shouldnt it look for Only local images are allowed. tags or something? Can you explain what it is supposed to do?

thanks!

Matt

BradM’s picture

Basically, from what I remember, the $pages variable contains a whole lot of info of the node that is being shown. The 0 line is the actual body content of the node. The $find string is a regular expression of sorts, anyway, it essentially seeks out any img tags in the node body.

So if on page 0 (as in the main page, not the node itself, and not to be confused with pages[0]) the script searches the node body for an image tag. If it finds one, it displays it in the teaser. I've got my teaser formatted in such a way that any images are automatically shrunk, using CSS.

A working example can be seen here:
http://www.vampyres.ca/portal/article/from_fear_to_fascination

Can you fill me in with *how* it's not working? Just not showing an image, or any error messages present? I'm really not much of a coding guru, but I might be able to help.

killerwhale65’s picture

ok, $pages[0] does not seem to exist. What variable do i need to use them for the full content?

totalsense’s picture

subscribe

Dabitch’s picture

subscribe

Rob T’s picture

This sounds interesting. I may be in need of this in the near future.