adjacent_image_link

Displays next or previous image link that has the same post parent.

Description

adjacent_image_link( (constant) $prev = true, (string) $size = 'thumbnail', (bool) $text = false ); 

Retrieves the current attachment object from the $post global.

Parameters (3)

0. $prev — Optional. (constant) => true
Whether to display the next (false) or previous (true) link. Default true.
1. $size — Optional. (string) => 'thumbnail'
Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default thumbnail..
2. $text — Optional. (bool) => false
Link text. Default false.

Usage

  1. if ( !function_exists( 'adjacent_image_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Optional. Whether to display the next (false) or previous (true) link. Default true. 
  5. $prev = true; 
  6.  
  7. // Optional. Image size. Accepts any valid image size, or an array of width and height 
  8. // values in pixels (in that order). Default 'thumbnail'. 
  9. $size = 'thumbnail'; 
  10.  
  11. // Optional. Link text. Default false. 
  12. $text = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = adjacent_image_link($prev, $size, $text); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { 
  2. $post = get_post(); 
  3. $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' ) ) ); 
  4.  
  5. foreach ( $attachments as $k => $attachment ) { 
  6. if ( $attachment->ID == $post->ID ) { 
  7. break; 
  8.  
  9. $output = ''; 
  10. $attachment_id = 0; 
  11.  
  12. if ( $attachments ) { 
  13. $k = $prev ? $k - 1 : $k + 1; 
  14.  
  15. if ( isset( $attachments[ $k ] ) ) { 
  16. $attachment_id = $attachments[ $k ]->ID; 
  17. $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); 
  18.  
  19. $adjacent = $prev ? 'previous' : 'next'; 
  20.  
  21. /** 
  22. * Filters the adjacent image link. 
  23. * The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency,  
  24. * either 'next', or 'previous'. 
  25. * @since 3.5.0 
  26. * @param string $output Adjacent image HTML markup. 
  27. * @param int $attachment_id Attachment ID 
  28. * @param string $size Image size. 
  29. * @param string $text Link text. 
  30. */ 
  31. echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );