-
-
Save maheshwaghmare/02b5611b48f1e7ce397cd191bd44a2cf to your computer and use it in GitHub Desktop.
Build parent and child page tree - https://maheshwaghmare.com/?p=37686
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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