Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress navigation for siblings
<?php
/*
* Based on https://gist.github.com/LL782/3551634
*/
function wp_links_siblings($postID = null){
if(!$postID){
global $post;
$postID = $post->ID;
}
$ancestors = get_post_ancestors( $postID );
/* Get the top Level page->ID count base 1, array base 0 so -1 */
$parent = ($ancestors) ? $ancestors[0]: $postID;
$pagelist = get_pages('child_of='. $parent .'&sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = ( isset($pages[$current-1]) ) ? $pages[$current-1] : '';
$nextID = ( isset($pages[$current+1]) ) ? $pages[$current+1] : '';
return [
'next' => $nextID,
'prev' => $prevID
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment