From 5697b2f828073af396fd47be90c8c58de628aae1 Mon Sep 17 00:00:00 2001 From: Lance Willett Date: Wed, 27 Mar 2013 20:37:34 +0000 Subject: [PATCH] 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. git-svn-id: http://core.svn.wordpress.org/trunk@23826 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-content/themes/twentyten/functions.php | 40 +++++++++++++++++++++++ wp-content/themes/twentyten/loop.php | 7 ++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/wp-content/themes/twentyten/functions.php b/wp-content/themes/twentyten/functions.php index ea5f8c5244..955b727f4b 100644 --- a/wp-content/themes/twentyten/functions.php +++ b/wp-content/themes/twentyten/functions.php @@ -514,3 +514,43 @@ function twentyten_posted_in() { ); } endif; + +/** + * Retrieves the IDs for images in a gallery. + * + * @uses get_post_galleries() first, if available. Falls back to shortcode parsing, + * then as last option uses a get_posts() call. + * + * @since Twenty Ten 1.6. + * + * @return array List of image IDs from the post gallery. + */ +function twentyten_get_gallery_images() { + $images = array(); + + if ( function_exists( 'get_post_gallery_images' ) ) { + $galleries = get_post_galleries(); + if ( isset( $galleries[0]['ids'] ) ) + $images = explode( ',', $galleries[0]['ids'] ); + } else { + $pattern = get_shortcode_regex(); + preg_match( "/$pattern/s", get_the_content(), $match ); + $atts = shortcode_parse_atts( $match[3] ); + if ( isset( $atts['ids'] ) ) + $images = explode( ',', $atts['ids'] ); + } + + if ( ! $images ) { + $images = get_posts( array( + 'fields' => 'ids', + 'numberposts' => 999, + 'order' => 'ASC', + 'orderby' => 'menu_order', + 'post_mime_type' => 'image', + 'post_parent' => get_the_ID(), + 'post_type' => 'attachment', + ) ); + } + + return $images; +} diff --git a/wp-content/themes/twentyten/loop.php b/wp-content/themes/twentyten/loop.php index 082ee00990..6f8b247352 100644 --- a/wp-content/themes/twentyten/loop.php +++ b/wp-content/themes/twentyten/loop.php @@ -70,20 +70,19 @@ $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) ); + $images = twentyten_get_gallery_images(); if ( $images ) : $total_images = count( $images ); $image = array_shift( $images ); - $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' ); ?>

%2$s photo.', 'This gallery contains %2$s photos.', $total_images, 'twentyten' ), 'href="' . get_permalink() . '" title="' . esc_attr( sprintf( __( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"', number_format_i18n( $total_images ) ); ?>

- +