Skip to content

Instantly share code, notes, and snippets.

@billerickson billerickson/functions.php
Last active Aug 29, 2015

Embed
What would you like to do?
<?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
You can’t perform that action at this time.