Numbered siblings (child page navigation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// the menu_order parameter has been added to account for custom page ordering | |
function numbered_siblings($link) { | |
global $post; | |
$siblings = get_pages('child_of=' . $post->post_parent . '&parent=' . $post->post_parent . '&sort_column=menu_order'); | |
foreach($siblings as $key=>$sibling) { | |
if($post->ID == $sibling->ID) { | |
$ID = $key; | |
} | |
} | |
$closest = array( | |
'before' => get_permalink($siblings[$ID-1]->ID), | |
'after' => get_permalink($siblings[$ID+1]->ID) | |
); | |
if($link == 'before' || $link == 'after') { | |
return $closest[$link]; | |
} | |
else { | |
return $closest; | |
} | |
} | |
function numbered_navigation($parent, $previous, $next) { | |
global $post; | |
$post_slug = $post->post_name; | |
$count = 0; $current = 0; | |
$pages = get_pages('child_of=' . $parent . '&depth=1&sort_column=menu_order'); | |
$out = ''; | |
$out .= '<div class="navigation"> | |
<a href="' . numbered_siblings('before') . '">' . $previous . '</a> | |
<a href="' . numbered_siblings('after') . '">' . $next . '</a> | |
<div class="page-index">'; | |
foreach($pages as $page) { | |
$count++; | |
if($post_slug == $page->post_name) | |
$current = $count; | |
} | |
$out .= $current . '/' . $count; | |
$out .= '</div> | |
</div>'; | |
return $out; | |
} | |
// Usage | |
echo numbered_navigation('', 'Previous', 'Next'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment