Skip to content

Instantly share code, notes, and snippets.

@mwender
Created September 23, 2013 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwender/6676322 to your computer and use it in GitHub Desktop.
Save mwender/6676322 to your computer and use it in GitHub Desktop.
Retrieves next/prev links for WordPress pages or 'heirarchical' post_types.
global $post;
$pageID = $post->ID;
$args = array(
'parent' => 0,
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
);
$pagelist = get_pages( $args );
$pages = array();
$x = 0;
foreach( $pagelist as $page ){
$pages[$x] = $page;
if( $page->ID == $pageID )
$current = $x;
$x++;
}
$prevID = $pages[$current - 1]->ID;
$nextID = $pages[$current + 1]->ID;
echo '<div class="navigation">';
if( !empty( $prevID ) )
echo '<a href="' . get_permalink( $prevID ) . '" class="btn">Previous: ' . get_the_title( $prevID ) . '</a>';
if( !empty( $nextID ) )
echo '<a href="' . get_permalink( $nextID ) . '" class="btn">Next: ' . get_the_title( $nextID ) . '</a>';
echo '</div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment