Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.