I have installed this plugin (http://wordpress.org/plugins/media-categories-2/) to categorize my images.
In my functions.php I have put the following code to limit the number of images shown in the gallery:
function get_random_gallery_images(){ global $wpdb,$post; $ids = ""; $counter = 0; $number_of_posts = 1; $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'orderby' => 'rand', 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { if ($counter != 0) { $ids .= ','.$attachment->ID; } else { $ids .= $attachment->ID; } $counter++; } } return $ids; }
in my single.php I have put the following code:
$attachment_ids = get_random_gallery_images(); $category_current = get_the_category($post->ID) ; //for getting the images of that specific category echo do_shortcode('[gallery columns="1" category="'.$category_current[0]->name.'" include="'.$attachment_ids.'" link="file"]');
The problem is that is does not matter if I change the values of numberposts. It always displays all of the images. What I am doing wrong? I only want to display 1 image.