Make WordPress Core

Changeset 23826


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

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

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

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/twentyten/functions.php

    r23778 r23826  
    515515}
    516516endif;
     517
     518/**
     519 * Retrieves the IDs for images in a gallery.
     520 *
     521 * @uses get_post_galleries() first, if available. Falls back to shortcode parsing,
     522 * then as last option uses a get_posts() call.
     523 *
     524 * @since Twenty Ten 1.6.
     525 *
     526 * @return array List of image IDs from the post gallery.
     527 */
     528function twentyten_get_gallery_images() {
     529    $images = array();
     530
     531    if ( function_exists( 'get_post_gallery_images' ) ) {
     532        $galleries = get_post_galleries();
     533        if ( isset( $galleries[0]['ids'] ) )
     534            $images = explode( ',', $galleries[0]['ids'] );
     535    } else {
     536        $pattern = get_shortcode_regex();
     537        preg_match( "/$pattern/s", get_the_content(), $match );
     538        $atts = shortcode_parse_atts( $match[3] );
     539        if ( isset( $atts['ids'] ) )
     540            $images = explode( ',', $atts['ids'] );
     541    }
     542
     543    if ( ! $images ) {
     544        $images = get_posts( array(
     545            'fields'         => 'ids',
     546            'numberposts'    => 999,
     547            'order'          => 'ASC',
     548            'orderby'        => 'menu_order',
     549            'post_mime_type' => 'image',
     550            'post_parent'    => get_the_ID(),
     551            'post_type'      => 'attachment',
     552        ) );
     553    }
     554
     555    return $images;
     556}
  • trunk/wp-content/themes/twentyten/loop.php

    r23793 r23826  
    7171<?php else : ?>
    7272                <?php
    73                     $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
     73                    $images = twentyten_get_gallery_images();
    7474                    if ( $images ) :
    7575                        $total_images = count( $images );
    7676                        $image = array_shift( $images );
    77                         $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    7877                ?>
    7978                        <div class="gallery-thumb">
    80                             <a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
     79                            <a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image( $image, 'thumbnail' ); ?></a>
    8180                        </div><!-- .gallery-thumb -->
    8281                        <p><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, 'twentyten' ),
     
    8483                                number_format_i18n( $total_images )
    8584                            ); ?></em></p>
    86                 <?php endif; ?>
     85                <?php endif; // end twentyten_get_gallery_images() check ?>
    8786                        <?php the_excerpt(); ?>
    8887<?php endif; ?>
Note: See TracChangeset for help on using the changeset viewer.