Skip to content

Instantly share code, notes, and snippets.

@wolffe
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolffe/bc40ccd2d41f78144dca to your computer and use it in GitHub Desktop.
Save wolffe/bc40ccd2d41f78144dca to your computer and use it in GitHub Desktop.
Numbered siblings (child page navigation)
<?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