The “memory exhausted” error in question happened because of this part in your function: 'parent' => $next_page -> post_parent
, which should actually be 'parent' => $next_page -> ID
(or I’d omit the unnecessary spaces, i.e. I’d use 'parent' => $next_page->ID
).
And the error can be illustrated like so:
$cur_page = get_post( 123 ); // assume the post_parent is 45
aiv_get_sibling_pages( $cur_page );
// 1. get_pages() is called with 'parent' set to 45.
// 2. Assume get_pages() returned page 123 as the 1st item.
// 3. Your `foreach` calls aiv_get_sibling_pages( <the post object for page 123> )
// 4. .. and then the loop restarts at step 1 and never ends..
// eventually, PHP exits with the "memory exhausted" error :(
So as I said earlier, use 'parent' => $next_page->ID
and the memory error would be gone.
But I would also make these changes:
-
Before the
$out .= '<ol>';
, add this:// Don't add the OL if there are no more pages. if ( empty( $pages ) ) { return ''; }
-
The
get_pages()
in theforeach
is not necessary. So,// Replace this: $child_pages = get_pages('child_of=" . $page->ID); if(count($child_pages) > 0) { $out .= aiv_get_sibling_pages($next_page); } // with just: $out .= aiv_get_sibling_pages( $next_page );
And actually, you could use wp_list_pages()
..
Something like this:
$list = wp_list_pages( array(
"child_of' => 123, // just change 123 to the correct ID
'title_li' => '',
'echo' => false,
) );
echo '<ol>' . str_replace(
[ '<ul>', '<ul ', '</ul>' ],
[ '<ol>', '<ol ', '</ol>' ],
$list
) . '</ol>';
But yes, if you want full control over the HTML, then your custom function would be easier in that case.