Sep172009

GetImages

This is what I use on my blog to automagically pull out the first 3 images in the content of the blog post and display them with Ditto


<?php
//If no documents then return
if(!isset($ids)){ return; }

//limit the number of images outputted
$field = (isset($number))? $number : 3;

//Make an array of all ids
$docArray = explode(',',$ids);
$docs = $modx->getDocuments($docArray, 1, 0,"id,content");

//For each document

foreach ($docs as $doc){
    $html .= $doc['content'];
    preg_match_all('/<img[^>]+>/i',$html, $images); 
    //print_r($images);

    //This should append $images to a bigger array
}

for($i=0; $i<$number; $i += 1){
    $output .= $images[0][$i];
}


return $output;
?>

Write a comment


If you have trouble reading the code, click on the code itself to generate a new random code.