Skip to content

Working with StartBox

If you have seen my Tweets or have read the news on here recently, then you know I have been using a WordPress framework called StartBox. I have been extremely pleased with it and Brian who is the creator of StartBox has been very helpful.

I am currently working on a new website that needs to load a random image from a set of image on page load. I thought I could make use of the current slideshow feature that StartBox has and adapt it to load one image instead of loading all images into a slideshow.

First of all I tried changing the shortcode for the slideshow to output the random image. I just removed the for each loop and added code to get a random item in the array. Don’t worry if you don’t want to know how I modified it, you can simply copy and paste the following code into your functions.php file in your child theme if you have StartBox.

// Handle Shortcode
function sb_randomimage_shortcode($atts, $content = NULL) {
	extract(shortcode_atts(array('id' => 0), $atts));

	$size = get_post_meta($id, 'size', true);

	switch($size) {
		case 'max':
			$dimensions = get_post_meta($id, 'size_max', true);
		case 'custom':
			$dimensions = get_post_meta($id, 'size_custom', true);
		case 'min':
			$dimensions = get_post_meta($id, 'size_min', true);

	$slides = get_post_meta($id, 'slide', false);
	usort($slides, 'sb_slide_sort'); // order the elements of the array
	$total = count($slides);
	$random = (mt_rand()%$total);

	$result = '';
	$result .= '
'; $slide = $slides[$random]; $result .= sb_post_image( $dimensions['width'], $dimensions['height'], null, 1, array( 'image_id' => $slide['attachment_id'], 'title' => $slide['caption'], 'alt' => $slide['caption'], 'echo' => false ) ); $result .= '
'; $result .= ''; return $result; } add_shortcode('randomimage', 'sb_randomimage_shortcode');

It will allow you to use a shortcode like this in any post or page (without the space after the [ ):

[ randomimage id="123"]

This works fine but I wanted to save having a duplicate shortcode for the sake of it, so I began updating the plugin Brian created in StartBox.

Now when I add a Slideshow the options panel has the option to show a random image from the set on page load.

By default the slideshow is on but if you choose Random Image then it will hide the slideshow options and leave the size option, like in the example image. You can still set the size but instead of the animated slideshow it will only load one image every time the page is loaded.

If this is useful to you then here is the slideshows.php file for you to download and use yourself. This works with the latest version of StartBox which is version

Random Image slideshows.php file for StartBox

You will need to download the file which I have zipped and extract the slideshows.php file. Then using ftp you will need to upload it to your website into the startbox theme. The folder the file needs to go in is in startbox > includes > plugins. There will already be a file there with the same name. To be safe, make sure you backup the original or rename it for now and then upload the new slideshows.php file.

Once uploaded you can use the original shortcode for the slideshows e.g:

[ slideshow id="123"]

You never know Brian may think this is useful and include it in the next release, but for now follow these instructions and let me know how you get on. If you have any trouble just put the original file back or delete the new one and set the old file back to the correct name.

Although this is working fine on my site please make sure you test this before using it on a live website. You are using the file at your own risk.