Skip to content

Instantly share code, notes, and snippets.

@isuke01
Created July 23, 2018 11:55
Show Gist options
  • Save isuke01/9fd895b5e53d516c8c9e9214ad7c8c38 to your computer and use it in GitHub Desktop.
Save isuke01/9fd895b5e53d516c8c9e9214ad7c8c38 to your computer and use it in GitHub Desktop.
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