WordPress.org

Make WordPress Core

Ticket #23044: 24688.diff

File 24688.diff, 2.4 KB (added by andy, 10 months ago)
  • media.php

     
    10801080 * Retrieves the current attachment object from the $post global. 
    10811081 * 
    10821082 * @since 2.5.0 
     1083 * @uses _previous_image_posts_where_filter_once() 
     1084 * @uses _next_image_posts_where_filter_once() 
    10831085 * 
    10841086 * @param bool $prev Optional. Default is true to display previous link, false for next. 
    10851087 */ 
    10861088function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { 
     1089        $adjacent = $prev ? 'previous' : 'next'; 
     1090        $order = $prev ? 'DESC' : 'ASC'; 
    10871091        $post = get_post(); 
    1088         $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) ); 
     1092        add_filter( 'posts_where', "_{$adjacent}_image_posts_where_filter_once" ); 
     1093        $attachment = array_pop( get_children( array( 
     1094                'post_parent' => $post->post_parent, 
     1095                'post_status' => 'inherit', 
     1096                'post_type' => 'attachment', 
     1097                'post_mime_type' => 'image', 
     1098                'orderby' => "menu_order $order, ID $order", 
     1099                'posts_per_page' => 1, 
     1100                'suppress_filters' => false, 
     1101        ) ) ); 
    10891102 
    1090         foreach ( $attachments as $k => $attachment ) 
    1091                 if ( $attachment->ID == $post->ID ) 
    1092                         break; 
    1093  
    1094         $k = $prev ? $k - 1 : $k + 1; 
    1095  
    10961103        $output = $attachment_id = null; 
    1097         if ( isset( $attachments[ $k ] ) ) { 
    1098                 $attachment_id = $attachments[ $k ]->ID; 
     1104        if ( isset( $attachment ) ) { 
     1105                $attachment_id = $attachment->ID; 
    10991106                $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); 
    11001107        } 
    11011108 
    1102         $adjacent = $prev ? 'previous' : 'next'; 
    11031109        echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); 
    11041110} 
    11051111 
    11061112/** 
     1113 * Append to WHERE clause for selecting only the previous image. 
     1114 */ 
     1115function _previous_image_posts_where_filter_once( $where ) { 
     1116        remove_filter( 'posts_where', __FUNCTION__ ); 
     1117        $post = get_post(); 
     1118        return "$where AND (menu_order < $post->menu_order OR (menu_order = $post->menu_order AND ID < $post->ID))"; 
     1119} 
     1120 
     1121/** 
     1122 * Append to WHERE clause for selecting only the next image. 
     1123 */ 
     1124function _next_image_posts_where_filter_once( $where ) { 
     1125        remove_filter( 'posts_where', __FUNCTION__ ); 
     1126        $post = get_post(); 
     1127        return "$where AND (menu_order > $post->menu_order OR (menu_order = $post->menu_order AND ID > $post->ID))"; 
     1128} 
     1129 
     1130/** 
    11071131 * Retrieve taxonomies attached to the attachment. 
    11081132 * 
    11091133 * @since 2.5.0