Last active
April 8, 2022 23:07
-
-
Save LL782/3551634 to your computer and use it in GitHub Desktop.
Next and Prev Page Siblings - Wordpress
This file contains 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
@quasiDigi thanks for pointing that out! I've edited the file now. Cheers for your input 👍