WordPress 父页面中显示子页面列表

WordPress21.5K阅读模式

如果想在WordPress 父页面中显示子页面列表链接,可以通过下面的代码实现。

WordPress 父页面中显示子页面列表将下面的代码添加到当前主题 functions.php 中:

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(); ?>

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

 

原文:How to Use Shortcodes in your WordPress Sidebar Widgets

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  2  访客  2
    • MAY的SEO博客
      MAY的SEO博客 3

      这个没有看得太懂,要是博主用图片展示一下效果就更完美了~

      • 不想登录
        不想登录 0

        页面模板中函数wpb_list_child_pages()不显示。需要改为echo wpb_list_child_pages()才能正确显示。

      匿名

      发表评论

      匿名网友
      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

      拖动滑块以完成验证