wordpress使用短代码实现在父页面中显示子页面列表链接

  • 时间:2020-05-16 19:29:42
  • 分类:网络文摘
  • 阅读:179 次

下面的图片是让 wordpress 在父页面中显示子页面列表链接的一个演示图。想要实现此效果,可以这样做:

显示子页面列表链接

添加如下代码到当前 wordpress主题的函数模版 functions.php 文件中。

  1. function wpb_list_child_pages() {
  2.     global $post;
  3.     if ( is_page() && $post->post_parent )
  4.         $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
  5.     else
  6.         $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
  7.     if ( $childpages ) {
  8.         $string = '<ul>' . $childpages . '</ul>';
  9.     }
  10.     return $string;
  11. }
  12. add_shortcode('wpb_childpages', 'wpb_list_child_pages');

之后,可以使用短代码:

  1. [wpb_childpages]

加到文本小工具中。

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

  1. add_filter('widget_text', 'do_shortcode');

添加到当前主题 functions.php 中,让文本小工具支持短代码。

也可以将下面的代码添加到主题页面模板适当的位置:

  1. <?php wpb_list_child_pages(); ?>

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

原文:https://zmingcx.com/display-a-sublist-in-the-wordpress-parent-page.html

推荐阅读:
Digital Relationships: 5 Tips For Building A Better Veterinary B  The 2020 Pandemic Further Boosts Earnings and Influence of Blogg  What to Do When Your Blog Gets Too Big for You Alone  Minimum Numbers of Function Calls to Make Target Array  Harddrives will fail – it is just a matter of when  Bruteforce/DFS/Backtracking Algorithm using Javascript to Solve   DFS and BFS Algorithm to Find Numbers With Same Consecutive Diff  How to Remove Items/Entries with Specific Values from Map/HashMa  Find the Real Root of 4^x + 6^x = 9^x  Depth First Search (Backtracking) Algorithm to Solve a Sudoku Ga 
评论列表
添加评论