Well, I found about two dozens of different plugins and other crap that promised to extract the first image of every post in Wordpress. This feature was crucial for my upcoming portfolio webpage, so I went on digging deeper. Thankfully I found the Blog Mum blog which gave me a profound yet creative solution. However, I had to have the height and width of my images(divs with background actually) embedded into the div’s style in order for my slider to work correctly. Thats when I made some adjustments to the code. This is my first time messing with wordpress script on this level so please excuse my PHP.
Functions.php hase this:
// Retrieve the src of first image in a post
function bm_extract_string($start, $end, $original) { $original = stristr($original, $start); $trimmed = stristr($original, $end); return substr($original, strlen($start), -strlen($trimmed)); } function getFirstImage() { $content = get_the_content(); $pic_string = bm_extract_string(’src=”‘,’” ‘,$content); $imagesize = getimagesize($pic_string); list($width, $height, $type, $attr) = getimagesize($pic_string); echo ‘< a href=”’.$link.’” style=”background:url(‘.$pic_string.’); display:block; width:’.$width.’px; height:’.$height.’px;”></a> ‘; }
and inside your index.php simply put this:
<?php getFirstImage(); ?>
posted : Friday, August 7th, 2009
Reblog Notes Show
Tags : wordpress, php, embed image, first image, extract image, extract first image, get first image, getFirstImage, plugin, plug-in, more,