Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Extract a random image from the last 3 in a post gallery
- *
- * @param string $size the image size . default to thumbnail default WP size
- * @return array image data or false.
- */
- if ( ! function_exists( 'get_rand_img_from_post_gallery' ) ){
- function get_rand_img_from_post_gallery($size = 'thumbnail') {
- global $post;
- $retarray = array();
- $images = get_children(array(
- 'post_parent' => $post->ID,
- 'post_type' => 'attachment',
- 'numberposts' => -1, // show all
- 'post_status' => null,
- 'post_mime_type' => 'image',
- 'orderby' => 'menu_order',
- 'order' => 'DESC'
- ));
- if (is_array($images) && count($images)>0){
- //preserve associative array keys: php > 5.0.2 only!!!
- $last_three_images= array_slice($images,0,3,true);
- $random = $last_three_images[array_rand($last_three_images,1)];
- $retarray['id'] = $random->ID;
- $retarray['imgtag'] = wp_get_attachment_image($random->ID,$size);
- $retarray['post-link'] = get_permalink($random->post_parent);
- $retarray['title'] = apply_filters('the_title',$random->post_title);
- return $retarray;
- }else{
- //no images found
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement