Random Featured ItemGallery

I’ve added a featured item gallery to the index page of my theme. It currently loops through the 5 most recent featured items. However, I have close to 50 items marked as featured. Is there any way to randomly pull 5 of these items each time the index page is accessed? I’ve been stumped by this problem for a while.

Here’s what I’ve been working with:

<div id="featured-gallery" class="galleryview">
	<ul class="gallery">
		//this loops the 5 most recent featured items
		$items = get_items(array('featured'=>true, 'recent'=>true), 5);
		while(loop_items()): ?>
			<?php $index = 0; ?>
			<?php while ($file = loop_files_for_item()): ?>
			    <?php if ($file->hasfullsize()): ?>
			    <!-- this makes sure the loop grabs only the first image for the item -->
			        <?php if ($index == 0): ?>
		    	       <?php echo /*Image URL*/'<li>';
			echo '<div class="panel-content">';
			echo /*Image URL*/link_to_item('<img src="'.item_file('fullsize uri').'" title="'.item('Dublin Core', 'Title').'"/>');
			echo '<div class="panel-overlay">';
			echo /*Item Title*/'<p><h3>'.link_to_item().'</h3></p></div></div></li>'; ?>
		    	    <?php endif; ?>
			    <?php endif; ?>
			<?php endwhile;?>

			<?php endwhile; ?></ul>

Would love to do this too! Please keep us up to date with what you come up with :)

Unfortunately right now there is no way to pass random as an order to get_items, and likewise there is no way to set a limit beyond 1 in display_random_featured_item. This is something likely to change in future releases.

It is possible, though you would need to write your own helper function to do it, with a custom items query.

In the meantime, I've created tickets to add helpers for displaying random featured items, and for adding a method to return results randomly for any item query.

Thanks! I'll see if I can come up with anything.

I figured out a way to do this:

		//this creates an array of the featured items
		$items = get_items(array('featured'=>true),50);
		//this randomizes the order of the array of the featured items
		//this breaks the array into groups of 5
		$subset = array_chunk($items,5);
		//this loops through the first group of 5

The rest of the code is the same as I posted at the beginning of this thread. I'm not sure if there is a better way to do this, but this works.

Hi Andy,

To elaborate on my suggestion for a custom helper function: Here's a function I just wrote that queries the database directly for a specific number of featured items, and returns those results randomly. This uses some methods from Omeka Db and Zend to build the select statement.

You can copy/paste this function to your theme's custom.php file, and use it somewhere on your theme like you would get_items(), then loop through the results. To get five random featured items with images, for example:

$items = custom_get_random_featured_items('5');

I added a 'custom_' prefix to the function I wrote to prevent any possible naming collisions with helper functions we end up adding to Omeka, though you could rename it to whatever you'd prefer. Keep in mind that we'll likely add some similar methods to the Item table in the Omeka core, but you could use this in the meantime.

Hope this helps!

awesome! thanks Jeremy.

Thank you very much.