Skip to content

Instantly share code, notes, and snippets.

@cherihung
Created August 4, 2013 23:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cherihung/49a65b31348fe9729eac to your computer and use it in GitHub Desktop.
Save cherihung/49a65b31348fe9729eac to your computer and use it in GitHub Desktop.
create prev/next nav for custom post type. something that works like this plugin http://wordpress.org/plugins/previous-and-next-post-in-same-taxonomy/ but can offer order by menu order
//from this post http://bucketpress.com/next-and-previous-post-link-in-same-custom-taxonomy
// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order title',
'order' => 'ASC',
'post_type' => 'your_custom_post_type',
'your_custom_taxonomy' => 'your_custom_taxonomy_term'
);
$postlist = get_posts( $postlist_args );
// get ids of posts retrieved from get_posts
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same taxonomy
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
if ( !empty($previd) ) {
echo '<a rel="prev" href="' . get_permalink($previd). '">previous</a>';
}
if ( !empty($nextid) ) {
echo '<a rel="next" href="' . get_permalink($nextid). '">next</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment