Make WordPress Core

Changeset 23824


Ignore:
Timestamp:
03/27/2013 08:34:28 PM (12 years ago)
Author:
lancewillett
Message:

Twenty Eleven: improve how gallery image IDs are retrieved for use in the Gallery post format template. Props to obenland for original patch, fixes #22907.

Location:
trunk/wp-content/themes/twentyeleven
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/twentyeleven/content-gallery.php

    r22199 r23824  
    3131            <?php if ( post_password_required() ) : ?>
    3232                <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
    33 
    34             <?php else : ?>
    35                 <?php
    36                     $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    37                     if ( $images ) :
    38                         $total_images = count( $images );
    39                         $image = array_shift( $images );
    40                         $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    41                 ?>
    42 
     33            <?php else :
     34                $images = twentyeleven_get_gallery_images();
     35                if ( $images ) :
     36                    $total_images = count( $images );
     37                    $image = array_shift( $images );
     38            ?>
    4339                <figure class="gallery-thumb">
    44                     <a href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
     40                    <a href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image( $image, 'thumbnail' ); ?></a>
    4541                </figure><!-- .gallery-thumb -->
    4642
     
    4945                        number_format_i18n( $total_images )
    5046                    ); ?></em></p>
    51             <?php endif; ?>
     47            <?php endif; // end twentyeleven_get_gallery_images() check ?>
    5248            <?php the_excerpt(); ?>
    5349        <?php endif; ?>
  • trunk/wp-content/themes/twentyeleven/functions.php

    r23792 r23824  
    627627}
    628628add_filter( 'body_class', 'twentyeleven_body_classes' );
     629
     630/**
     631 * Retrieves the IDs for images in a gallery.
     632 *
     633 * @uses get_post_galleries() first, if available. Falls back to shortcode parsing,
     634 * then as last option uses a get_posts() call.
     635 *
     636 * @since Twenty Eleven 1.6.
     637 *
     638 * @return array List of image IDs from the post gallery.
     639 */
     640function twentyeleven_get_gallery_images() {
     641    $images = array();
     642
     643    if ( function_exists( 'get_post_gallery_images' ) ) {
     644        $galleries = get_post_galleries();
     645        if ( isset( $galleries[0]['ids'] ) )
     646            $images = explode( ',', $galleries[0]['ids'] );
     647    } else {
     648        $pattern = get_shortcode_regex();
     649        preg_match( "/$pattern/s", get_the_content(), $match );
     650        $atts = shortcode_parse_atts( $match[3] );
     651        if ( isset( $atts['ids'] ) )
     652            $images = explode( ',', $atts['ids'] );
     653    }
     654
     655    if ( ! $images ) {
     656        $images = get_posts( array(
     657            'fields'         => 'ids',
     658            'numberposts'    => 999,
     659            'order'          => 'ASC',
     660            'orderby'        => 'menu_order',
     661            'post_mime_type' => 'image',
     662            'post_parent'    => get_the_ID(),
     663            'post_type'      => 'attachment',
     664        ) );
     665    }
     666
     667    return $images;
     668}
Note: See TracChangeset for help on using the changeset viewer.