Make WordPress Core

Ticket #23617: 23617.1.diff

File 23617.1.diff, 3.0 KB (added by lancewillett, 12 years ago)

Move to function, provide fallback for missing IDs

  • wp-content/themes/twentyten/functions.php

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

     
    7070                                <?php the_content(); ?>
    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' ),
    8382                                                                'href="' . get_permalink() . '" title="' . esc_attr( sprintf( __( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"',
    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; ?>
    8988                        </div><!-- .entry-content -->