Advertisement
5ally

(WordPress) [wpb_childpages]

Oct 10th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. (See https://stackoverflow.com/questions/49183161/show-child-pages-of-specific-parent-on-any-page-in-wordpress/ for details.)
  2.  
  3. STEP 1: Add these to your theme's functions.php file:
  4. --
  5.  
  6. function wpb_list_child_pages( $atts = array() ) {
  7. $atts = shortcode_atts( array(
  8. 'id' => 0, // The parent post's ID. Set this or the slug.
  9. 'slug' => '', // The parent post's slug. Set this or the ID.
  10. 'menu_toggle' => 0, // Enable menu's toggle link? 1 = yes; 0 = no.
  11. 'show_menu' => 1, // Show the menu by default? 1 = yes; 0 = no.
  12.  
  13. // By default, the toggle link's label is "Toggle Menu"; if you want a
  14. // different label, set the menu_toggle to the label you prefer. E.g.
  15. // [wpb_childpages slug="parent-page" menu_toggle="Show/Hide Menu"]
  16.  
  17. // If menu_toggle is disabled (i.e. 0), the show_menu is "ignored".
  18. ), $atts );
  19.  
  20. if ( $atts['slug'] ) {
  21. global $wpdb;
  22.  
  23. $q = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $atts['slug'] );
  24. $post_id = $wpdb->get_var( $q );
  25.  
  26. if ( ! $post_id ) {
  27. return '';
  28. }
  29. } else {
  30. $post_id = absint( $atts['id'] );
  31. }
  32.  
  33. $post = get_post( $post_id ); // WP_Post on success.
  34. $childpages = '';
  35.  
  36. if ( $post && is_post_type_hierarchical( $post->post_type ) ) {
  37. $childpages = wp_list_pages( array(
  38. 'child_of' => $post->ID,
  39. 'title_li' => '',
  40. 'echo' => 0,
  41. ) );
  42. }
  43.  
  44. $html = '';
  45. if ( $childpages ) {
  46. // This class is required for the JS functionality.
  47. $class = 'wpb-child-pages';
  48.  
  49. $menu_style = '';
  50. if ( $atts['menu_toggle'] && $atts['show_menu'] ) {
  51. $class .= ' menu-shown';
  52. } elseif ( $atts['menu_toggle'] ) {
  53. $class .= ' menu-hidden';
  54. $menu_style = 'display: none;';
  55. }
  56.  
  57. $html = '<div class="' . $class . '">';
  58.  
  59. if ( $atts['menu_toggle'] ) {
  60. $label = ( '1' != $atts['menu_toggle'] ) ?
  61. $atts['menu_toggle'] : 'Toggle Menu';
  62.  
  63. // Add the link for showing or hiding the menu.
  64. $html .= '<a href="#" class="toggle-menu">' . $label . '</a>';
  65. }
  66.  
  67. // Add the menu.
  68. $menu_style = $menu_style ? ' style="' . $menu_style . '"' : '';
  69. $html .= '<ul' . $menu_style . '>' . $childpages . '</ul>';
  70.  
  71. $html .= '</div>';
  72.  
  73. static $js_added = false;
  74. // Add the inline JS code which handles the toggling stuff.
  75. if ( ! $js_added && $atts['menu_toggle'] && wp_script_is( 'jquery' ) ) {
  76. add_action( 'wp_print_footer_scripts', 'wpb_list_child_pages_script' );
  77. $js_added = true;
  78. }
  79. }
  80.  
  81. return $html;
  82. }
  83. add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
  84.  
  85. function wpb_list_child_pages_script() {
  86. ?>
  87. <script>
  88. jQuery( function( $ ){
  89. $( '.wpb-child-pages' ).on( 'click', '.toggle-menu', function( e ){
  90. e.preventDefault();
  91.  
  92. // Find the menu.
  93. var $wrapr = $( this ).closest( '.wpb-child-pages' ),
  94. $menu = $wrapr.children( 'ul' );
  95.  
  96. // Toggle the menu.
  97. if ( $menu.is( ':visible' ) ) {
  98. $menu.slideUp();
  99. $wrapr.removeClass( 'menu-shown' );
  100. } else {
  101. $menu.slideDown();
  102. $wrapr.addClass( 'menu-shown' );
  103. }
  104. } );
  105. } );
  106. </script>
  107. <?php
  108. }
  109.  
  110. STEP 2: Usage examples:
  111. --
  112.  
  113. 1. Use the parent post's ID or slug:
  114.  
  115. [wpb_childpages id="123"]
  116. [wpb_childpages slug="parent-page"]
  117.  
  118. 2. The menu's toggle link is disabled *by default*; so to enable it, set the
  119. menu_toggle parameter to 1, like so:
  120.  
  121. [wpb_childpages id="123" menu_toggle="1"]
  122.  
  123. 3. The menu is shown/expanded by default; so to hide it by default, set the
  124. show_menu parameter to 0, like so:
  125.  
  126. [wpb_childpages slug="parent-page" menu_toggle="1" show_menu="0"]
  127.  
  128. 4. To use a custom label for the menu's toggle link, set the menu_toggle to
  129. the label that you prefer. Example:
  130.  
  131. [wpb_childpages slug="parent-page" menu_toggle="Show/Hide Menu"]
  132.  
  133. Note that show_menu is "ignored" if the menu_toggle is 0 (or empty).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement