Site icon Hip-Hop Website Design and Development

create shortcode to show children if any otherwise siblings

I’m trying to make a shortcode I can put into a widget that will show the siblings if there are any otherwise show the children.

Here is my code I’ve started with:

//sidebar siblings menu
function rt_list_children() {
    global $post;
    $page = $post->ID;
    if ( $post->post_parent ) {
        $page = $post->post_parent;
    }
    $children = wp_list_pages( array(
        'child_of' => $page,
        'title_li'    => '',
        'echo' => '0',
    ) );

    if ($children) {
       $output = '<ul>';
       $output .= $children;
       $output .= '</ul>';
    } else {
       $output = '';
    }
    return $output;
}
add_shortcode ('sidebar-menu','rt_list_children');

Then, in a html widget, I paste [sidebar-menu].

Right now, The menu only shows up on pages that are children and have siblings. It shows the siblings.

Ideally, I’d like if it’s a top level page without children, don’t show.

If it’s a top level page with children, show the children.

If it’s a child page with siblings, show the siblings.

Is this close?

Maybe there’s a way to do this without creating parents, but by simply using the menu i’ve setup in appearances/menu and it will only show the options for children i’ve set there?