Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active August 29, 2015 13:57
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 billerickson/9398979 to your computer and use it in GitHub Desktop.
Save billerickson/9398979 to your computer and use it in GitHub Desktop.
<?php
/**
* Get Page Hierarchy
*
* returns array
* -- pages (all pages in order)
* -- prev (previous page)
* -- next (next page)
*
* @author Bill Erickson
* @link http://www.billerickson.net/code/get-page-hierarchy/
*/
function be_get_page_hierarchy() {
global $post;
$loop = new WP_Query( array(
'post_type' => 'page',
'post_parent' => $post->post_parent,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'fields' => 'ids',
) );
$output = array(
'pages' => $loop->posts,
);
foreach( $loop->posts as $k => $v ) {
if( $v == get_the_ID() ) {
if( isset( $loop->posts[$k-1] ) )
$output['prev'] = $loop->posts[$k-1];
if( isset( $loop->posts[$k+1] ) )
$output['next'] = $loop->posts[$k+1];
}
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment