admin管理员组文章数量:1300203
I have this code to list my parent page and all pages under it on the sidebar. It shows both parent and child pages on first two levels but when I open a third level page, it only shows the current page on the sidebar. How can I modify this to show parent page and all child pages, no matter how many levels of child pages I have.
CODE:
<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'depth' => 0,
'child_of' => $post->post_parent,
'echo' => 0
));
} else {
$children = wp_list_pages( array(
'title_li' => '',
'depth' => 0,
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<ul class="sideNavigation">
<?php echo $children; ?>
</ul>
<?php endif; ?>
I have this code to list my parent page and all pages under it on the sidebar. It shows both parent and child pages on first two levels but when I open a third level page, it only shows the current page on the sidebar. How can I modify this to show parent page and all child pages, no matter how many levels of child pages I have.
CODE:
<?php
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'depth' => 0,
'child_of' => $post->post_parent,
'echo' => 0
));
} else {
$children = wp_list_pages( array(
'title_li' => '',
'depth' => 0,
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<ul class="sideNavigation">
<?php echo $children; ?>
</ul>
<?php endif; ?>
Share
asked Mar 24, 2021 at 7:12
absolutelygraduallyabsolutelygradually
111 bronze badge
1 Answer
Reset to default 0Use this WP function get_pages() and get_page_children()
function get_child_pages( $parent_page_ID ){
$all_pages = get_pages( array( 'post_type'=> 'page' ) );
$child_pages = get_page_children( $parent_page_ID, $all_pages );
if( !empty( $child_pages ) ){
$html .= '<ul>';
foreach ( $child_pages as $key => $child_page ) {
$html .= '<li>'.$child_page->post_title;
get_child_pages( $child_page->ID );
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
function list_pages(){
$html = '';
if ( $post->post_parent ) {
$parent = $post->post_parent;
}else{
$parent = $post->ID;
}
$parent_pages = get_pages( array( 'parent' => $parent, 'post_type'=> 'page' ) );
$html.= '<ul>';
foreach ( $parent_pages as $parent_page ) {
$html .= '<li>'.$parent_page->post_title;
$html .= get_child_pages( $parent_page->ID );
$html .= '</li>';
}
$html.= '</ul>';
return $html;
}
add_shortcode( 'list_pages', 'list_pages' );
本文标签: navigationwplistpages to show all pages on all sub pages
版权声明:本文标题:navigation - wp_list_pages to show all pages on all sub pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741657057a2390821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论