Skip to content

Instantly share code, notes, and snippets.

@eccentricpixel
Last active August 29, 2015 14:11
Show Gist options
  • Save eccentricpixel/d7583daf38e7ee69e75a to your computer and use it in GitHub Desktop.
Save eccentricpixel/d7583daf38e7ee69e75a to your computer and use it in GitHub Desktop.
Efficient way to generate prev and next links for navigation purposes, to traverse between records in a table/pod/post-type. Assumes you are using PODS framework.
============================
/***** a more complex way to get adjacent records based on some parameters *****/
THE CODE
============================
<?php
// grab the id for the current post
$theBookId = get_the_ID();
// grab some related taxonomy to filter by
$terms = wp_get_post_terms( $theBookId, 'country' );
foreach ( $terms as $term ) {
$relatedCountry = $term->slug;
}
// get next/prev records within a pod from a detail page view
$theSlugs = array();
$names = array();
$theArt = array();
$burl = get_bloginfo('url')."/book/";
$theSlug = pods_url_variable();
//Get the pods data and load it into an array
$Record = pods('book');
$RecordParams = array('limit'=>-1, 'orderby'=>'release_date ASC', 'where'=>"country.slug='$relatedCountry'");
$Record->find($RecordParams);
$count = $Record->getTotalRows();
$i = 0;
while($Record->fetch()){
// if you want to make the pres/next links more creative, you could things like grab graphics from the adjacent posts and use them as backgrounds or whatever. Below is a way to handle a 'marquee_artwork' field. You would declare it in your link with something like <img src="'.$theArt[$ppos].'"> ($ppos for prev and $npos for next) Obviously remove these art variables if you aren't going to use them.
$i++;
$theSlugs[$i] = $Record->field('slug');
$names[$i] = $Record->field('post_title');
$art = $Record->field('marquee_artwork.guid');
$theArt[$i] = $art[0];
}
// in case nothing is defined, show nothing:
$prev_link = '';
$next_link = '';
$page_no = 1;
$Record->resetPointer(0);
//See if something is defined. If not, links go to the base url defined above ($burl)
$key = array_search($theSlug, $theSlugs);
if ($key) {
$ppos = 0 == $key ? $count : $key-1;
$prev_link = '<a href="'.$burl.$theSlugs[$ppos].'" class="prevBook" title="'.$names[$ppos].'">View<span>Prev</span></a>';
$npos = $count == $key ? 0 : $key+1;
$next_link = '<a href="'.$burl.$theSlugs[$npos].'" class="nextBook" title="'.$names[$npos].'">View<span>Next</span></a>';
$page_no = $key+1;
$Record->resetPointer($key);
}
?>
============================
THE MARKUP
============================
<div class="bookNavigation">
<?php if($prev_link){ echo $prev_link; } ?>
<?php if($next_link){ echo $next_link; } ?>
</div>
============================
ALTERNATE METHOD
just gets adjacent items in table - not really the best approach because entries are not necessarily going to be entered into the database in sequence/order. See first method for more accurate results.
============================
<?php
global $post;
$post_slug = $post->post_name;
$theCurrentId = get_the_ID();
$postType = 'appearances'; // declare what table you want
$thePod = pods($postType);
$next = $thePod->next_id($theCurrentId);
$prev = $thePod->prev_id($theCurrentId);
$nextItem = pods($postType, $next);
$nextLink = $nextItem->field('permalink');
$prevItem = pods($postType, $prev);
$prevLink = $prevItem->field('permalink');
?>
============================
THE MARKUP
============================
<?php if($prev) {?>
<div class="left"><a href="<?php echo $prevLink; ?>">View<span>Prev</span></a></div>
<?php } ?>
<?php if($next) { ?>
<div class="right"><a href="<?php echo $nextLink; ?>">View<span>Next</span></a></div>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment