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 badges1 Answer
Reset to default 2Yes, 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
版权声明:本文标题:wp list pages - create shortcode to show children if any otherwise siblings 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216339a2360114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论