voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Swap two feature images randomly on page load. #47

Closed joshuaiz closed 9 years ago

joshuaiz commented 10 years ago

Not an issue but just some code to alternate two feature images at random on page load.

Use this within the loop:

<?php
// Get a random number
$min = 1;
$max = 10;
$num = rand($min,$max);

// If it is an odd number and we have a feature image

if ( $num & 1 && has_post_thumbnail() ) { 

  the_post_thumbnail(); 

   } elseif ( $num % 2 == 0 && MultiPostThumbnails::has_post_thumbnail( get_post_type(), 'secondary-image', NULL) ) { 

    // If it is an even number and we have a secondary image 

MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image', NULL, ''); // add your thumbnail size as the last argument

  } else { // It's even and we don't have a second feature image so get the default feature image

  if ( has_post_thumbnail() ) { 

 the_post_thumbnail();

  } 

}
?>