admin管理员组

文章数量:1278721

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?

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?

Share Improve this question asked Mar 13, 2019 at 7:36 rudtekrudtek 6,3535 gold badges30 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Yes, you're close. What you want is something like this:

function rt_list_children() {
    global $post;
    $output = '';

    if ( $post->post_parent ) {  // if it's a child page
        $siblings = wp_list_pages( array(
            'child_of' => $post->post_parent,
            'title_li'    => '',
            'echo' => '0',
        ) );
        if ( $siblings ) {  // it has siblings
            $children = $siblings;
        } else {  // it has no siblings, so show its children
            $children = wp_list_pages( array(
                'child_of' => $post->ID,
                'title_li'    => '',
                'echo' => '0',
            ) );
        }
    } else {  // it's a top level page, show its children
        $children = wp_list_pages( array(
            'child_of' => $post->ID,
            'title_li'    => '',
            'echo' => '0',
        ) );
    }

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

本文标签: wp list pagescreate shortcode to show children if any otherwise siblings