-
-
Save quasiDigi/45827b5d48d71ef07c2ebc9da60fe337 to your computer and use it in GitHub Desktop.
Next and Prev Page Siblings - Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
>< 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 ></a> | |
</div> | |
<?php endif; ?> | |
</nav> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed the old $pages variables in lines 12 and 13 by the actual $siblingIds variable