Skip to content

Instantly share code, notes, and snippets.

@LL782
Last active April 8, 2022 23:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LL782/3551634 to your computer and use it in GitHub Desktop.
Save LL782/3551634 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; ?>
@LL782
Copy link
Author

LL782 commented Oct 18, 2019

Hey @simplyjerett I don't understand what you're trying to do

@LL782
Copy link
Author

LL782 commented Oct 18, 2019

I've updated it so pagination only shows for sibling pages. If the page doesn't have a parent it won't show pagination. This is simpler to understand and more inline with the name of the function next-prev-siblings and the description in Gist.

@ejusa
Copy link

ejusa commented Jan 2, 2020

Very excited to have found this. Could you help me figure out how I adapt for a custom post type (resource)?

I tried to do this using just the simple Next and Previous Pages code and adding if ($post->post_parent == 0){$prevID = NULL;} but I still need to somehow make $nextID null if it does not have a parent, and I cannot figure out how to do that within my function.

function next_prev_page_publication () {
	global $post;
	$pagelist = get_pages(array(
    		'post_type'    => 'resource',
		'sort_column' => 'menu_order',
		'sort_order' => 'asc')
		);
	$pages = array();
	foreach ($pagelist as $page) {
		$pages[] += $page->ID;
		}
	$current = array_search(get_the_ID(), $pages);
	$prevID = $pages[$current-1];
	$nextID = $pages[$current+1];
	if ($post->post_parent == 0){$prevID = NULL;}
		?>
		<div class="navigation">
			<?php 
                         if (!empty($prevID)) { ?>
			      <div class="alignleft">
			            <a href="<?php echo get_permalink($prevID); ?>"title="<?php echo get_the_title($prevID); ?>">Previous</a>
			      </div>
		        <?php }
	                if ( !empty($nextID) ) { ?>
		               <div class="alignright">
			             <a href="<?php echo get_permalink($nextID); ?>"title="<?php echo get_the_title($nextID); ?>">Next</a>
			       </div>
			<?php } ?>
		</div>
         <!-- .navigation -->
	<?php }

@simplyjerett
Copy link

Ultimately I ended up going a different route with this. My apologies but you can try to hit up LL782 to see if he can help you with this.

@quasiDigi
Copy link

hi @LL782,

I think there is a small errata in the code, on lines 12 and 13:

$prevId = ( isset($pages[$current-1]) ) ? $pages[$current-1] : '';
$nextId = ( isset($pages[$current+1]) ) ? $pages[$current+1] : '';

should be:

$prevId = ( isset($siblingIds[$current-1]) ) ? $siblingIds[$current-1] : '';
$nextId = ( isset($siblingIds[$current+1]) ) ? $siblingIds[$current+1] : '';

Thank you for share 👍

@LL782
Copy link
Author

LL782 commented Sep 23, 2020 via email

@quasiDigi
Copy link

@LL782, I made a fork but can't find a way to make you a PR over here. Probably because it's a single file and not a repository. It's only two words that need to be changed. You actually forgot to change some variable names last time you adapted the code.

@LL782
Copy link
Author

LL782 commented Oct 21, 2020

@quasiDigi thanks for pointing that out! I've edited the file now. Cheers for your input 👍

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