Last active
August 29, 2015 14:01
-
-
Save scofennell/84b6458c5be2daa80f57 to your computer and use it in GitHub Desktop.
WordPress function to get adjacent gallery images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a link to the next/prev post in a gallery | |
* | |
* @param string $next_prev Specify if the function should return the value for the next or the prev post | |
* @return string A link to the next/prev post in a gallery | |
*/ | |
function sjf_deh_get_adjacent_image_link( $next_prev = 'prev' ){ | |
// The current post. | |
$post = get_post(); | |
// If the post has no parent, bail. | |
$post_parent = absint( $post->post_parent ); | |
if( empty( $post_parent ) ) { return false; } | |
// If the post is not an attachment, bail. | |
if( $post->post_type != 'attachment' ) { return false; } | |
// Args for a get_posts call to get siblings of the current post. | |
$args = array( | |
// The parent of the current post. | |
'post_parent' => $post->post_parent, | |
// So as to allow attachments. | |
'post_status' => 'any', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image/jpeg', | |
// Order by recency. | |
'order' => 'DESC', | |
'orderby' => 'date', | |
// Get all posts. | |
'posts_per_page' => -1, | |
); | |
// Get the posts as an array, and convert it to a numerically indexed array so we can grab the next/prev | |
$attachments = array_values( get_posts( $args ) ); | |
// The number of attachments. | |
$num_attachments = count($attachments); | |
// The index of the last attachment (we decrement because numeric arrays start with 0). | |
$last = $num_attachments - 1; | |
// Counts which array key we're on as we loop. We'll use this value to grab the next/prev post in our numeric array. | |
$i=0; | |
// For each attachment... | |
foreach ( $attachments as $k => $attachment ) { | |
// If we hit the current post ... | |
if ( $attachment->ID == $post->ID ) { | |
// If we want the previous post ... | |
if( $next_prev == 'prev' ){ | |
// Decrement the counter. | |
$i--; | |
// If there is a previous post, grab it. | |
if( isset($attachments[$i]) ) { | |
$adjacent_image = $attachments[$i]; | |
$icon = 'icon-chevron-left'; | |
$title_attr = 'Previous Image'; | |
// If there is not a previous post, it's because we're on the first post, so grab the final image | |
} else { | |
$adjacent_image = $attachments["$last"]; | |
$icon = 'icon-chevron-sign-left'; | |
$title_attr = 'Final Image'; | |
} | |
// Exit the foreach loop. | |
break; | |
// If we want the next post ... | |
} else { | |
// Increment the counter. | |
$i++; | |
// If there is a next post, grab it. | |
if( isset( $attachments[$i] ) ) { | |
$adjacent_image = $attachments[$i]; | |
$icon = 'icon-chevron-right'; | |
$title_attr = 'Next Image'; | |
// If there is not a next post, it's because we're on the last post, so grab the first post. | |
} else { | |
$adjacent_image = $attachments[0]; | |
$icon = 'icon-chevron-sign-right'; | |
$title_attr = 'First Image'; | |
} | |
// Exit the foreach loop. | |
break; | |
} | |
} | |
$i++; | |
} // End foreach post. | |
// Grab the href for the adjacent post | |
$adjacent_image_id = absint( $adjacent_image->ID ); | |
$href = esc_url( get_attachment_link( $adjacent_image_id ) ); | |
// Complete the output by adding a FontAwesome | |
$out = "<a title='$title_attr' href='$href'><i class='$icon'></i> </a>"; | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment