Loading
0

WordPress父页面显示子页面列表方法

如果想在WordPress父页面显示子页面列表链接,该如何实现呢?今天就为大家分享一下WordPress父页面显示子页面列表方法,只要通过以下的简单代码就可轻松实现。将下面的代码添加到当前主题functions.php中:

WordPress父页面显示子页面列表方法

function wpb_list_child_pages() {

global $post;

if ( is_page() && $post->post_parent )

$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' );

if ( $childpages ) {

$string = '<ul>' . $childpages . '</ul>';

}

return $string;

}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

然后,使用短代码:

[wpb_childpages]

加到文本小工具中,默认文本小工具不支持短代码,可以将:

add_filter('widget_text', 'do_shortcode');

添加到当前主题functions.php中,让文本小工具支持短代码。也可以将下面的代码添加到主题页面模板适当的位置:

<?php wpb_list_child_pages(); ?>

是不是很简单!比如新建一个页面模板,将代码加进去,只在使用该功能时,选择新建的页面模板。