Make WordPress Core


Ignore:
Timestamp:
06/03/2013 09:44:25 PM (13 years ago)
Author:
lancewillett
Message:

Twenty Thirteen: Clean up image attachment template and move logic to twentythirteen_the_attached_image() function. Props obenland, fixes #24479.

File:
1 edited

Legend:

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

    r24393 r24402  
    421421endif;
    422422
     423if ( ! function_exists( 'twentythirteen_the_attached_image' ) ) :
     424/**
     425 * Prints the attached image with a link to the next attached image.
     426 *
     427 * @since Twenty Thirteen 1.0
     428 *
     429 * @return void
     430 */
     431function twentythirteen_the_attached_image() {
     432    $post                = get_post();
     433    $attachment_size     = apply_filters( 'twentythirteen_attachment_size', array( 724, 724 ) );
     434    $next_attachment_url = wp_get_attachment_url();
     435
     436    /**
     437     * Grab the IDs of all the image attachments in a gallery so we can get the URL
     438     * of the next adjacent image in a gallery, or the first image (if we're
     439     * looking at the last image in a gallery), or, in a gallery of one, just the
     440     * link to that image file.
     441     */
     442    $attachments = array_values( get_children( array(
     443        'post_parent'    => $post->post_parent,
     444        'post_status'    => 'inherit',
     445        'post_type'      => 'attachment',
     446        'post_mime_type' => 'image',
     447        'order'          => 'ASC',
     448        'orderby'        => 'menu_order ID'
     449    ) ) );
     450
     451    // If there is more than 1 attachment in a gallery...
     452    if ( count( $attachments ) > 1 ) {
     453        foreach ( $attachments as $k => $attachment ) {
     454            if ( $attachment->ID == $post->ID )
     455                break;
     456        }
     457        $k++;
     458
     459        // get the URL of the next image attachment...
     460        if ( isset( $attachments[ $k ] ) )
     461            $next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
     462
     463        // or get the URL of the first image attachment.
     464        else
     465            $next_attachment_url = get_attachment_link( $attachments[0]->ID );
     466    }
     467
     468    printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
     469        esc_url( $next_attachment_url ),
     470        the_title_attribute( array( 'echo' => false ) ),
     471        wp_get_attachment_image( $post->ID, $attachment_size )
     472    );
     473}
     474endif;
     475
    423476/**
    424477 * Returns the URL from the post.
Note: See TracChangeset for help on using the changeset viewer.