Created
October 22, 2014 16:43
-
-
Save whatnickcodes/4c00c106e40860c2e640 to your computer and use it in GitHub Desktop.
WordPress custom post types prev and next logic
This file contains 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 | |
$projects = array(); | |
$loop = new WP_Query(array('post_type' => array('work'), 'posts_per_page' => -1, 'order' => 'ASC')); | |
$i = 1; | |
$current = FALSE; | |
$current_post_id = $post->ID; | |
while ($loop->have_posts()) : $loop->the_post(); | |
$projects[$i]['title'] = get_field('grid_title'); | |
$projects[$i]['url'] = get_permalink(); | |
if ($current_post_id == $post->ID) { | |
$current = $i; | |
} | |
$i++; | |
endwhile; | |
wp_reset_postdata(); | |
$count = count($projects); | |
// First in lineup | |
if ($current == 1) | |
{ | |
$prev = $projects[$current + 1]; | |
$next = $projects[$count]; | |
} | |
// Last in lineup | |
elseif ($current == $count) | |
{ | |
$prev = $projects[1]; | |
$next = $projects[$count - 1]; | |
} | |
// Normal | |
else | |
{ | |
$prev = $projects[$current + 1]; | |
$next = $projects[$current - 1]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment