Support » Fixing WordPress » Grab a list of excluded Images from [Gallery] shortcode

  • Something just isn’t clicking, and I would love for someone to show me just how silly I’m being by not getting this.

    What I’m trying to achieve: I have some posts that are nothing but a gallery shortcode, showing quite a few images, but I would rather not show 50-100 images on the index page, so I’m trying to create a custom “excerpt” for these posts that only show the first 3 images, with a link to view more.

    What I have: in my functions.php I have

    function gallery_shortcode_exists(){
        global $post;
        # Check the content for an instance of [gallery] with or without arguments
        $pattern = get_shortcode_regex();
            preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
            && array_key_exists( 2, $matches )
            && in_array( 'gallery', $matches[2] )
            return true;
        # Sourced from

    to check if the gallery shortcode exists. Then, in my loop I have

    <?php if( ( is_category() || is_archive() || is_home() ) && gallery_shortcode_exists() ) { ?>
    		<?php		# Determine if the post_content column contains the string [gallery]
    			    # Get the first three attachments using the posts_per_page parameter
    			    $args = array(
    			        'post_type' => 'attachment',
    			        'post_mime_type' => 'image',
    					'order'=> 'ASC',
    			        'posts_per_page' => 3,
    			        'post_parent' => get_the_ID(),
    			    $attachments = get_children( $args );
    				$count = count( $attachments );
    			    # If any attachments are returned, proceed
    			    if( $attachments ){
    			        # Spin cycle to collate attachment IDs
    			        foreach( $attachments as $attachment )
    			        		$includes[] = $attachment->ID;
    							$links = 'file';
    			        # Format our IDs in a comma-delimited string
    			        $includes = implode(',', $includes);
    			        # Inject your include argument
    					$shortcode = "[gallery include='$includes' link='$links']";
    					<?php echo $shortcode;?><br />
    					<?php echo get_the_content();?><br />
    			      <?php  # Render the Gallery using the standard editorial input syntax
    			        echo do_shortcode($shortcode);    // echo do_shortcode($shortcode);
    			        # Add a View More link
    					if( $count>=3 ){ ?>
    			       		<h3 class="ribbon"><?php echo '<a href="' . get_permalink() . '">' . __('View more', 'standard') . '</a>'; ?> </h3>
    			<?php } ?>
    			    <?php }
    			        _e('Foo Bar - No attachments found and no excerpt to display', 'standard');	?>
    		<?php } else { ?>
    			<?php the_content( __( 'Continue Reading...', 'standard' ) ); ?>
    		<?php } // end if/else ?>

    The Problem: my gallery shortcode includes 2 attributes that exclude images from the gallery. An example of the shortcode might look like this [gallery exclude="1" hide="2,3,6,8,11,14"]. As it is when foreach( $attachments as $attachment ) $includes[] = $attachment->ID; runs, it is including images 1, 2, and 3, all of which I would like to exclude from the “excerpt”

    What is the best way to grab the array of image IDs that are excluded and hidden, so I can shove them into a variable and add that variable to my $args before I grab the 3 images? Thank you for any help.

  • The topic ‘Grab a list of excluded Images from [Gallery] shortcode’ is closed to new replies.