-
-
Save LL782/3551634 to your computer and use it in GitHub Desktop.
<?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; ?> |
@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.
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.
Hey @simplyjerett I don't understand what you're trying to do
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.
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 }
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.
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, 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.
@quasiDigi thanks for pointing that out! I've edited the file now. Cheers for your input 👍
This is sort of working but the next skips straight to the child but previous does go back one at a time.