twentyfourteen_the_attached_image

Print the attached image with a link to the next attached image.

Description

twentyfourteen_the_attached_image(); 

Usage

  1. if ( !function_exists( 'twentyfourteen_the_attached_image' ) ) { 
  2. require_once '/functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = twentyfourteen_the_attached_image(); 
  7.  

Defined (1)

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

/functions.php  
  1. function twentyfourteen_the_attached_image() { 
  2. $post = get_post(); 
  3. /** 
  4. * Filter the default Twenty Fourteen attachment size. 
  5. * @since Twenty Fourteen 1.0 
  6. * @param array $dimensions { 
  7. * An array of height and width dimensions. 
  8. * @type int $height Height of the image in pixels. Default 810. 
  9. * @type int $width Width of the image in pixels. Default 810. 
  10. * } 
  11. */ 
  12. $attachment_size = apply_filters( 'twentyfourteen_attachment_size', array( 810, 810 ) ); 
  13. $next_attachment_url = wp_get_attachment_url(); 
  14.  
  15. if ( $post->post_parent ) : // Only look for attachments if this attachment has a parent object. 
  16. /** 
  17. * Grab the IDs of all the image attachments in a gallery so we can get the URL 
  18. * of the next adjacent image in a gallery, or the first image (if we're 
  19. * looking at the last image in a gallery), or, in a gallery of one, just the 
  20. * link to that image file. 
  21. */ 
  22. $attachment_ids = get_posts( array( 
  23. 'post_parent' => $post->post_parent,  
  24. 'fields' => 'ids',  
  25. 'numberposts' => -1,  
  26. 'post_status' => 'inherit',  
  27. 'post_type' => 'attachment',  
  28. 'post_mime_type' => 'image',  
  29. 'order' => 'ASC',  
  30. 'orderby' => 'menu_order ID',  
  31. ) ); 
  32.  
  33. // If there is more than 1 attachment in a gallery... 
  34. if ( count( $attachment_ids ) > 1 ) { 
  35. foreach ( $attachment_ids as $idx => $attachment_id ) { 
  36. if ( $attachment_id == $post->ID ) { 
  37. $next_id = $attachment_ids[ ( $idx + 1 ) % count( $attachment_ids ) ]; 
  38. break; 
  39.  
  40. // get the URL of the next image attachment... 
  41. if ( $next_id ) { 
  42. $next_attachment_url = get_attachment_link( $next_id ); 
  43.  
  44. // or get the URL of the first image attachment. 
  45. else { 
  46. $next_attachment_url = get_attachment_link( reset( $attachment_ids ) ); 
  47. endif; // end post->post_parent check 
  48.  
  49. printf( '<a href="%1$s" rel="attachment">%2$s</a>',  
  50. esc_url( $next_attachment_url ),  
  51. wp_get_attachment_image( $post->ID, $attachment_size ) 
  52. );