Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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