WordPress prev/next page navigation

Show 'previous' and 'next' links on current page:


<?php

$pages_args = array(

	'child_of' => $post->post_parent,

	'parent' => $post->post_parent,

	'sort_column' => 'menu_order,post_title',

	'sort_order' => 'asc'

);

$page_list = get_pages($pages_args);

$pages = array();

foreach ($page_list as $page) {

   $pages[] += $page->ID;

}



$current_num = array_search($post->ID, $pages);

$prev_id = $pages[$current_num - 1];

$next_id = $pages[$current_num + 1];

?>



<ul class="nav-wrap">

<?php if (!empty($prev_id)) : ?>

    <li class="nav-prev">

    	<a href="<?php echo get_permalink($prev_id); ?>">Previous: <?php echo get_the_title($prev_id); ?></a>

    </li>

<?php endif;

if (!empty($next_id)) : ?>

    <li class="nav-next">

    	<a href="<?php echo get_permalink($next_id); ?>">Next: <?php echo get_the_title($next_id); ?></a>

    </li>

<?php endif; ?>

</ul>

Leave a Comment