Skip to content

Instantly share code, notes, and snippets.

@quasiDigi
Forked from LL782/next-prev-sibling.php
Last active September 23, 2020 23:26
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 quasiDigi/45827b5d48d71ef07c2ebc9da60fe337 to your computer and use it in GitHub Desktop.
Save quasiDigi/45827b5d48d71ef07c2ebc9da60fe337 to your computer and use it in GitHub Desktop.
Next and Prev Page Siblings - Wordpress
<?php
function getId($page) { return $page->ID; }
$ancestors = get_post_ancestors( $post->ID );
$parent = ($ancestors) ? $ancestors[0] : null;
if ($parent) :
$siblingIds = array_map( "getId", get_pages('child_of='. $parent .'&sort_column=menu_order&sort_order=asc') );
$current = array_search( get_the_ID(), $siblingIds );
$prevId = ( isset($siblingIds[$current-1]) ) ? $siblingIds[$current-1] : '';
$nextId = ( isset($siblingIds[$current+1]) ) ? $siblingIds[$current+1] : '';
?>
<nav id="pagination">
<?php if (!empty($prevId)) : ?>
<div class="alignleft">
<a
href="<?php echo get_permalink($prevId); ?>"
title="<?php echo get_the_title($prevId); ?>"
class="previous-page"
>&lt;&nbsp;&nbsp;Previous</a>
</div>
<?php endif; ?>
<?php if (!empty($nextId)) : ?>
<div class="alignright">
<a
href="<?php echo get_permalink($nextId); ?>"
title="<?php echo get_the_title($nextId); ?>"
class="next-page"
>Next&nbsp;&nbsp;&gt;</a>
</div>
<?php endif; ?>
</nav>
<?php endif; ?>
@quasiDigi
Copy link
Author

Changed the old $pages variables in lines 12 and 13 by the actual $siblingIds variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment