Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created May 17, 2021 15:40
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 maheshwaghmare/02b5611b48f1e7ce397cd191bd44a2cf to your computer and use it in GitHub Desktop.
Save maheshwaghmare/02b5611b48f1e7ce397cd191bd44a2cf to your computer and use it in GitHub Desktop.
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