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

@simplyjerett wow it’s been a long time since I wrote this. Not the cleanest bit of code. Let me try to get my head back into it.

I can see one thing that could be tripping you up:

  • The variable I’ve called $parent varies depending on if the page you’re on has a parent or not. $pagelist could either be a set of siblings or children. If you're on a top level page, next and previous will potentially be child pages.

That may have been designed that way to allow users to click down into child pages but it’s a bit unexpected and maybe should be taken out.

@simplyjerett
Copy link

That does work but is there away to loop through the main parent instead of going to other pages? Example:

If you have

domain.com/ancestor/parent
--domain.com/ancestor/parent/child
--domain.com/ancestor/parent/child2
domain.com/ancestor/parent2

I would like the next and prev to ONLY loop through the ancestor - stopping on each parent then the children then the next parent but then stops after the last parent. Same in regards to prev - it will only go up to the first parent and then the button/link goes away. This may not be easily done. I can't seem to find it to save my life.

@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