Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build parent and child page tree - https://maheshwaghmare.com/?p=37686
<?php
/**
* Build the nested page tree
*
* @todo Change the `prefix_` with your own unique string.
*
* @param array $childs.
* @param int $parent_id.
* @return array
*/
if( ! function_exists( 'prefix_build_tree' ) ) :
function prefix_build_tree( $childs = array(), $parent_id = 0) {
$tree = array();
foreach ($childs as $child_id => $single_page ) {
$single_page = (array) $single_page;
if ($single_page[ 'post_parent' ] == $parent_id) {
$children = prefix_build_tree( $childs, $single_page['ID'] );
if ( $children ) {
$single_page['children'] = $children;
}
$tree[] = $single_page;
}
}
return $tree;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment