Basically you want to display a menu of child pages of “Sevice One” if you are in any child page of “Service One”, a menu of child pages of “Service Two” if you are in any chiild page of “Service Two”, and so on.
This can be done with the child_of
argument of wp_list_pages
. We can fill this parameter dynamically using get_post_ancestors
for current page and set the before last parent as the child_of
parameter (the last parent would be “Services” and the before last would be “Service One” if you are in a page child of “Service One”, “Service Two” if you are in a page child of “Service Two”, and so on).
To include the <span>
and execute the shortocodes in that place we can use link_before
and link_after
arguments.
For example:
function pages_menu() {
if( !is_page() ) {
return false;
}
$page_id = get_queried_object_id();
$ancestors = get_post_ancestors($page_id);
$ancestors_count = count($ancestors);
if( $ancestors_count > 1 ) {
//the last item in $ancestors will be the top parent page, that is "Services"
//but we want the before top parent ("Service One", "Service Two", etc)
$top_menu_page = $ancestors[$ancestors_count - 2];
} else {
//We are actually on one of our top menu pages ("Service One", "Service Two", etc)
$top_menu_page = $page_id;
}
$args = array(
'child_of' => $top_menu_page,
'link_before' => '<span class="icon '.do_shortcode('[some-custom-field-shortcode]').'"></span> <span class="text">',
'link_after' => '</span>',
);
wp_list_pages( $args );
}
Now you can use pages_menu()
to print this dynamic menu on any page.
About the classes. Current page already has the current_page_item
class as you need. Also, each page class attribute is filled with a complete set of classes representing the item, including page ID, classes for pages with children, with ancestors, parent/child of current page, etc. Inspect the output to make an idea.
(I don’t kmow the exact context where this code will be used. It may needs adjustment to full fit to your needs.)