WordPress.org

Make WordPress Core

Changeset 24402


Ignore:
Timestamp:
06/03/2013 09:44:25 PM (5 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.

Location:
trunk/wp-content/themes/twentythirteen
Files:
2 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.
  • trunk/wp-content/themes/twentythirteen/image.php

    r23758 r24402  
    1010 */
    1111
    12 the_post();
    13 
    14 /**
    15  * Grab the IDs of all the image attachments in a gallery so we can get the URL of the next adjacent image in a gallery,
    16  * or the first image (if we're looking at the last image in a gallery), or, in a gallery of one, just the link to that image file
    17  */
    18 $attachments = array_values( get_children( array(
    19     'post_parent' => $post->post_parent,
    20     'post_status' => 'inherit',
    21     'post_type' => 'attachment',
    22     'post_mime_type' => 'image',
    23     'order' => 'ASC',
    24     'orderby' => 'menu_order ID'
    25 ) ) );
    26 
    27 foreach ( $attachments as $k => $attachment ) :
    28 if ( $attachment->ID == $post->ID )
    29     break;
    30 endforeach;
    31 
    32 $k++;
    33 // If there is more than 1 attachment in a gallery
    34 if ( count( $attachments ) > 1 ) :
    35     if ( isset( $attachments[ $k ] ) ) :
    36         // get the URL of the next image attachment
    37         $next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
    38     else :
    39         // or get the URL of the first image attachment
    40         $next_attachment_url = get_attachment_link( $attachments[ 0 ]->ID );
    41     endif;
    42 else :
    43     // or, if there's only 1 image, get the URL of the image
    44     $next_attachment_url = wp_get_attachment_url();
    45 endif;
    46 
    4712get_header(); ?>
    4813
     
    5520                    <div class="entry-meta">
    5621                        <?php
    57                             $published_text  = __( '<span class="attachment-meta">Published on <time class="entry-date" datetime="%1$s">%2$s</time> in <a href="%3$s" title="Return to %4$s" rel="gallery">%5$s</a></span>', 'twentythirteen' );
     22                            $published_text = __( '<span class="attachment-meta">Published on <time class="entry-date" datetime="%1$s">%2$s</time> in <a href="%3$s" title="Return to %4$s" rel="gallery">%5$s</a></span>', 'twentythirteen' );
    5823                            $post_title = get_the_title( $post->post_parent );
    5924                            if ( empty( $post_title ) || 0 == $post->post_parent )
    60                                 $published_text  = '<span class="attachment-meta"><time class="entry-date" datetime="%1$s">%2$s</time></span>';
     25                                $published_text = '<span class="attachment-meta"><time class="entry-date" datetime="%1$s">%2$s</time></span>';
    6126
    6227                            printf( $published_text,
     
    7742                            );
    7843
    79                             edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
     44                            edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' );
     45                        ?>
    8046                    </div><!-- .entry-meta -->
    8147                </header><!-- .entry-header -->
     
    8955                    <div class="entry-attachment">
    9056                        <div class="attachment">
    91                             <a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php
    92                             $attachment_size = apply_filters( 'twentythirteen_attachment_size', array( 724, 724 ) );
    93                             echo wp_get_attachment_image( $post->ID, $attachment_size );
    94                             ?></a>
     57                            <?php twentythirteen_the_attached_image(); ?>
    9558
    96                             <?php if ( ! empty( $post->post_excerpt ) ) : ?>
     59                            <?php if ( has_excerpt() ) : ?>
    9760                            <div class="entry-caption">
    9861                                <?php the_excerpt(); ?>
     
    10063                            <?php endif; ?>
    10164                        </div><!-- .attachment -->
    102 
    10365                    </div><!-- .entry-attachment -->
    10466
     
    11173
    11274                </div><!-- .entry-content -->
    113 
    11475            </article><!-- #post -->
    11576
Note: See TracChangeset for help on using the changeset viewer.