Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have certain posts that are nothing but [gallery] with quite a few images in them. Is there a way to define an auto generated excerpt to only display the first three images for these posts on the index, so that users would have to click through to see the rest? Thank you.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You can do this pretty easily using the do_shortcode function.

Check if an instance of [gallery] exists in your post content.

Here's a simple function to drop in functions.php that checks the current post's content for the gallery shortcode:

function gallery_shortcode_exists(){

    global $post;

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

    # Sourced from http://codex.wordpress.org/Function_Reference/get_shortcode_regex
}

Use do_shortcode() to render your gallery.

You can use the following within the loop in your template files:

# Determine if the post_content column contains the string [gallery]
if( gallery_shortcode_exists() ){

    # Get the first three attachments using the posts_per_page parameter
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'posts_per_page' => 3,
        'post_parent' => get_the_ID()
    );
    $attachments = get_children( $args );

    # If any attachments are returned, proceed
    if( $attachments ){

        # Spin cycle to collate attachment IDs
        foreach( $attachments as $attachment )
            $includes[] = $attachment->ID;

        # Format our IDs in a comma-delimited string
        $includes = implode(',', $includes);

        # Inject your include argument
        $shortcode = str_replace('[gallery', "[gallery include='$includes' ", get_the_content());

        # Render the Gallery using the standard editorial input syntax
        echo do_shortcode($shortcode);

        # Add a View More link
        echo '<a href="' . get_permalink() . '">' . __('View more', 'domain') . '</a>';
    }
    else
        _e('Foo Bar - No attachments found and no excerpt to display', 'domain');
}
else
    # Whatever fallback you desire
    the_excerpt();
share|improve this answer
    
Some notes: I'd check gallery_shortcode_exists() OR 'gallery' === get_post_type(). Second: Why the heck are you using a query string? :) Third: No more filter? and fourth: counting in the condition check is slower than doing it right before. +1 anyway –  kaiser Oct 19 '12 at 11:22
    
Hm, after thinking about it again, maybe you should check for theme support gallery before checking for the post type. –  kaiser Oct 19 '12 at 11:37
    
Works great, but some of the images that were excluded in the gallery shortcode are showing up in the three on index page [gallery exclude="1,2,3" link="file"] file ids 1,2 and 3 would show up in the excerpt. Thank you for your help. –  sosukeinu Oct 19 '12 at 13:25
    
@kaiser Nothing was stated in the question about a gallery post type. This is only the native WP [gallery] shortcode. There was also no mention of a gallery post format, maybe I'm missing your point. Also, not sure what filter you're referring to... –  Brian Fegter Oct 19 '12 at 14:36
2  
I like @kaiser approach also, but unfortunately, I'm already invested in using posts and shortcodes. I tried your edits. here is my actual loop : pastebin.com/yyQWnh2A and unfortunately, I'm still getting excluded images in the exerpt. This is specifically happing with [gallery] that only has 2 images, and it's dropping the featured image into the excerpt, even though I have it excluded in the gallery shortcode. Thank you for your help on this. –  sosukeinu Oct 19 '12 at 15:01
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.