Skip to content

Commit

Permalink
Twenty Eleven: improve how gallery image IDs are retrieved for use in…
Browse files Browse the repository at this point in the history
… the Gallery post format template. Props to obenland for original patch, fixes #22907.

git-svn-id: http://core.svn.wordpress.org/trunk@23824 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
lancewillett committed Mar 27, 2013
1 parent cccb293 commit 6f5b38a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
20 changes: 8 additions & 12 deletions wp-content/themes/twentyeleven/content-gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@
<div class="entry-content">
<?php if ( post_password_required() ) : ?>
<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>

<?php else : ?>
<?php
$images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
if ( $images ) :
$total_images = count( $images );
$image = array_shift( $images );
$image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
?>

<?php else :
$images = twentyeleven_get_gallery_images();
if ( $images ) :
$total_images = count( $images );
$image = array_shift( $images );
?>
<figure class="gallery-thumb">
<a href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
<a href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image( $image, 'thumbnail' ); ?></a>
</figure><!-- .gallery-thumb -->

<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, 'twentyeleven' ),
'href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"',
number_format_i18n( $total_images )
); ?></em></p>
<?php endif; ?>
<?php endif; // end twentyeleven_get_gallery_images() check ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
Expand Down
40 changes: 40 additions & 0 deletions wp-content/themes/twentyeleven/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,43 @@ function twentyeleven_body_classes( $classes ) {
return $classes;
}
add_filter( 'body_class', 'twentyeleven_body_classes' );

/**
* 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 Eleven 1.6.
*
* @return array List of image IDs from the post gallery.
*/
function twentyeleven_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;
}

0 comments on commit 6f5b38a

Please sign in to comment.