Random posts WordPress plug-in as random image gallery generator

I was helping to customize one WordPress blog and there was desire to have sidebar gallery with random picture from the post that would link to the post. We were using Popular posts plug-in already, so we decided to use Random posts plug-in. The problem we encountered was with the posts, that did not contain any image. This resulted in empty square in place of the image. Not nice. I know, we should use dedicated plug-in, but …

In case all the posts have image, this will work fine.

<?php
echo "<div class=\"gallery\">";
echo "<h3 class=\"gallery\">Gallery</h3>";
echo "<ul>";
random_posts('limit=6&output_template=<li>
<a href={url}><img src={imagesrc:} ></img></a></li>');
echo "</ul>";
echo "</div>";
?>

But if there are posts containing no image, this code will produce empty square. Here is how we overcome “flaw” of this plug-in. (Yes, dedicated plug-in would be better…)

Firstly, we need get output of the random post plug-in redirecting output of the function to some variable.

<?php
ob_start();
random_posts('limit=12&output_template={url}|{imagesrc:0}|
                       {imagesrc:1}|{imagesrc:2}[]');
$rposts = ob_get_contents();
ob_end_clean();

We have chosen 12 posts even we need just 6 since there is (unknown) probability, that some of them will contain no picture and some of them even more. We are printing out 3 pictures (if exist) that are in post. The “|” and “[]” are used as separators that will not appear in links and can be later used to split the string to addresses of posts and its images. Like this.

$rposts = explode( '[]' , $rposts );
$count = count($rposts);
for ($i = 1; $i <=$count ; $i++) {
 $rposts[$i] = explode( '|', $rposts[$i]  );
}

First link splits string (output of the redirected buffer is string …) according the single posts and the the for cycle will split it into array(“post address”, “post image 1”, “post image 2”, “post image 3”). Now we need filter out void output and create pairs of post-post_image.

$pocet = count($rposts);
$data = array();
for ($i = 1; $i <=$pocet ; $i++) {
 for ($j = 1; $j <= count($rposts[$i]) ; $j++ ) {
   if( strrpos($rposts[$i][$j], "/" ) !== FALSE ) {
   $data[] = array('0' => $rposts[$i][0], '1' => $rposts[$i][$j], );
   }
 }
}

Now, we need to choose 6 pairs (images) that will be displayed. If less then 6 post-post_image pairs exist, then probably the blog is just starting and we display just some image more times. Not nice, but client was eager to post lot of post with lot of images and some of them were already repared, but just in case we check.

$count = count($data);
if ( $count >= 6 ) {
 $rndar = array_rand($data, 6);
} else {
 $rndar = array_rand($data, $count);
 for($i=0; $i < 6 - $pocet; $i++){
   $rndar[] = array_rand($data, 1);
 }
}
echo "<ul>";
for($i=0; $i<count($rndar); $i++){
 echo "<li><a href=\"",$data[$rndar[$i]][0],"\">
 <img src=\"",$data[$rndar[$i]][1],"\" style=\"width:110px;
 height:110px;\" /></a></li>";
}
echo "</ul>";
echo "</div>";
?>

Maybe not the best solution but worked for us. Then I found that other plug-in Post Image Gallery, but that one we might try other time.

Leave a Reply

Your email address will not be published. Required fields are marked *