admin管理员组文章数量:1026989
how can I list the parent item with the child item in WP?
I use this:
function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
$parent = get_the_title($post->post_parent);
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul><li>' . $parent;
$string .= '<ul>' . $childpages . '</ul>';
$string .= '</li></ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
but I need this:
<ul>
<li><a href="#">PARENT ITEM</a></li>
<li><a href="#">CHILD ITEM 1</a></li>
<li><a href="#">CHILD ITEM 2</a></li>
<li><a href="#">CHILD ITEM 3</a></li>
<li><a href="#">CHILD ITEM 4</a></li>
</ul>
how can I list the parent item with the child item in WP?
I use this:
function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
$parent = get_the_title($post->post_parent);
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul><li>' . $parent;
$string .= '<ul>' . $childpages . '</ul>';
$string .= '</li></ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
but I need this:
<ul>
<li><a href="#">PARENT ITEM</a></li>
<li><a href="#">CHILD ITEM 1</a></li>
<li><a href="#">CHILD ITEM 2</a></li>
<li><a href="#">CHILD ITEM 3</a></li>
<li><a href="#">CHILD ITEM 4</a></li>
</ul>
Share
Improve this question
asked Mar 25, 2019 at 21:31
matstilmatstil
1
1
- There are a bunch of ways to do this, but here's a previous answer... wordpress.stackexchange/questions/192895/… – Monkey Puzzle Commented Mar 25, 2019 at 23:23
1 Answer
Reset to default 0function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$parent = wp_list_pages( 'title_li=&include=' . $post->post_parent . '&echo=0' );
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
//$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul>'. $parent . $childpages . '</ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
OUTPUT ON TOP LEVEL PAGE
<ul>
<li class="page_item page-item-283">
<a href="http://192.168.8.200/www/parent-page/child-page-1/">Child Page 1</a>
</li>
<li class="page_item page-item-285">
<a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
</li>
<li class="page_item page-item-286">
<a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
</li>
</ul>
OUTPUT ON A CHILD PAGE
<ul>
<li class="page_item page-item-282 current_page_ancestor current_page_parent">
<a href="http://192.168.8.200/www/parent-page/">Parent page</a>
</li>
<li class="page_item page-item-283 current_page_item">
<a href="http://192.168.8.200/www/parent-page/child-page-1/" aria-current="page">Child Page 1</a>
</li>
<li class="page_item page-item-285">
<a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
</li>
<li class="page_item page-item-286">
<a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
</li>
</ul>
how can I list the parent item with the child item in WP?
I use this:
function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
$parent = get_the_title($post->post_parent);
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul><li>' . $parent;
$string .= '<ul>' . $childpages . '</ul>';
$string .= '</li></ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
but I need this:
<ul>
<li><a href="#">PARENT ITEM</a></li>
<li><a href="#">CHILD ITEM 1</a></li>
<li><a href="#">CHILD ITEM 2</a></li>
<li><a href="#">CHILD ITEM 3</a></li>
<li><a href="#">CHILD ITEM 4</a></li>
</ul>
how can I list the parent item with the child item in WP?
I use this:
function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
$parent = get_the_title($post->post_parent);
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul><li>' . $parent;
$string .= '<ul>' . $childpages . '</ul>';
$string .= '</li></ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
but I need this:
<ul>
<li><a href="#">PARENT ITEM</a></li>
<li><a href="#">CHILD ITEM 1</a></li>
<li><a href="#">CHILD ITEM 2</a></li>
<li><a href="#">CHILD ITEM 3</a></li>
<li><a href="#">CHILD ITEM 4</a></li>
</ul>
Share
Improve this question
asked Mar 25, 2019 at 21:31
matstilmatstil
1
1
- There are a bunch of ways to do this, but here's a previous answer... wordpress.stackexchange/questions/192895/… – Monkey Puzzle Commented Mar 25, 2019 at 23:23
1 Answer
Reset to default 0function wpb_list_child_pages() {
global $post;
$parent = "";
if ( is_page() && $post->post_parent ) {
$parent = wp_list_pages( 'title_li=&include=' . $post->post_parent . '&echo=0' );
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
} else {
$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
//$parent = $post->post_title;
}
if ( $childpages ) {
$string = '<ul>'. $parent . $childpages . '</ul>';
}
return $string;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
OUTPUT ON TOP LEVEL PAGE
<ul>
<li class="page_item page-item-283">
<a href="http://192.168.8.200/www/parent-page/child-page-1/">Child Page 1</a>
</li>
<li class="page_item page-item-285">
<a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
</li>
<li class="page_item page-item-286">
<a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
</li>
</ul>
OUTPUT ON A CHILD PAGE
<ul>
<li class="page_item page-item-282 current_page_ancestor current_page_parent">
<a href="http://192.168.8.200/www/parent-page/">Parent page</a>
</li>
<li class="page_item page-item-283 current_page_item">
<a href="http://192.168.8.200/www/parent-page/child-page-1/" aria-current="page">Child Page 1</a>
</li>
<li class="page_item page-item-285">
<a href="http://192.168.8.200/www/parent-page/child-page-2/">Child Page 2</a>
</li>
<li class="page_item page-item-286">
<a href="http://192.168.8.200/www/parent-page/child-page-3/">Child Page 3</a>
</li>
</ul>
本文标签: menusList parent item with all child item on child pages
版权声明:本文标题:menus - List parent item with all child item on child pages 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659354a2161794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论