Forums

Maximum (limit) number of images in Gallery Shortcode (5 posts)

  1. scdwb
    Member
    Posted 2 years ago #

    Hey,

    I'm using the WordPress inbuilt gallery short code within my theme to display images in the sidebar on the home page

    do_shortcode('[gallery columns="1" id="12"]'); ?>

    Everything is working fine but I really need to limit the amount of images that are displayed. This is simply a little gallery feature in the sidebar with a title "Images from our Gallery".

    Something like this would be ideal:

    do_shortcode('[gallery columns="1" id="12" maximum="5"]'); ?>

    Is there any code someone has written, that we can put in our theme functions file to enable a limit to the number of images displayed from the gallery.

    Many thanks!

  2. krugazul
    Member
    Posted 2 years ago #

    Hi scdwb

    I had the same problem but couldn't find an answer so i wrote one.

    Include the function in functions.php, for your application you will need to specify the post id to be used. Im using this inside my loop on the single page.

    Then call the function and use the attachment ids it returns to include in the gallery shortcode.

    $attachment_ids = get_random_gallery_images();
    [gallery columns="1" id="12" include="'.$attachment_ids.'"]

    function get_random_gallery_images(){
    	global $wpdb,$post;
    		$ids = "";
    		$counter = 0;
    		$number_of_posts = 2;
    		$args = array(
    		'post_type' => 'attachment',
    		'numberposts' => 2,
    		'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;
    }
  3. MACscr
    Member
    Posted 1 year ago #

    Maybe im doing something wrong since i havent slept in about 24 hours now, but no matter what i change that number of posts to, its still showing 5. Its also not changing the images on each refresh. Im using WP 3.0.1 and no caching plugins. Any ideas?

  4. sputnick3k
    Member
    Posted 1 year ago #

    Did you ever get this to work? I could really use this code.

  5. sputnick3k
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic