Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (See https://stackoverflow.com/questions/49183161/show-child-pages-of-specific-parent-on-any-page-in-wordpress/ for details.)
- STEP 1: Add these to your theme's functions.php file:
- --
- function wpb_list_child_pages( $atts = array() ) {
- $atts = shortcode_atts( array(
- 'id' => 0, // The parent post's ID. Set this or the slug.
- 'slug' => '', // The parent post's slug. Set this or the ID.
- 'menu_toggle' => 0, // Enable menu's toggle link? 1 = yes; 0 = no.
- 'show_menu' => 1, // Show the menu by default? 1 = yes; 0 = no.
- // By default, the toggle link's label is "Toggle Menu"; if you want a
- // different label, set the menu_toggle to the label you prefer. E.g.
- // [wpb_childpages slug="parent-page" menu_toggle="Show/Hide Menu"]
- // If menu_toggle is disabled (i.e. 0), the show_menu is "ignored".
- ), $atts );
- if ( $atts['slug'] ) {
- global $wpdb;
- $q = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $atts['slug'] );
- $post_id = $wpdb->get_var( $q );
- if ( ! $post_id ) {
- return '';
- }
- } else {
- $post_id = absint( $atts['id'] );
- }
- $post = get_post( $post_id ); // WP_Post on success.
- $childpages = '';
- if ( $post && is_post_type_hierarchical( $post->post_type ) ) {
- $childpages = wp_list_pages( array(
- 'child_of' => $post->ID,
- 'title_li' => '',
- 'echo' => 0,
- ) );
- }
- $html = '';
- if ( $childpages ) {
- // This class is required for the JS functionality.
- $class = 'wpb-child-pages';
- $menu_style = '';
- if ( $atts['menu_toggle'] && $atts['show_menu'] ) {
- $class .= ' menu-shown';
- } elseif ( $atts['menu_toggle'] ) {
- $class .= ' menu-hidden';
- $menu_style = 'display: none;';
- }
- $html = '<div class="' . $class . '">';
- if ( $atts['menu_toggle'] ) {
- $label = ( '1' != $atts['menu_toggle'] ) ?
- $atts['menu_toggle'] : 'Toggle Menu';
- // Add the link for showing or hiding the menu.
- $html .= '<a href="#" class="toggle-menu">' . $label . '</a>';
- }
- // Add the menu.
- $menu_style = $menu_style ? ' style="' . $menu_style . '"' : '';
- $html .= '<ul' . $menu_style . '>' . $childpages . '</ul>';
- $html .= '</div>';
- static $js_added = false;
- // Add the inline JS code which handles the toggling stuff.
- if ( ! $js_added && $atts['menu_toggle'] && wp_script_is( 'jquery' ) ) {
- add_action( 'wp_print_footer_scripts', 'wpb_list_child_pages_script' );
- $js_added = true;
- }
- }
- return $html;
- }
- add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
- function wpb_list_child_pages_script() {
- ?>
- <script>
- jQuery( function( $ ){
- $( '.wpb-child-pages' ).on( 'click', '.toggle-menu', function( e ){
- e.preventDefault();
- // Find the menu.
- var $wrapr = $( this ).closest( '.wpb-child-pages' ),
- $menu = $wrapr.children( 'ul' );
- // Toggle the menu.
- if ( $menu.is( ':visible' ) ) {
- $menu.slideUp();
- $wrapr.removeClass( 'menu-shown' );
- } else {
- $menu.slideDown();
- $wrapr.addClass( 'menu-shown' );
- }
- } );
- } );
- </script>
- <?php
- }
- STEP 2: Usage examples:
- --
- 1. Use the parent post's ID or slug:
- [wpb_childpages id="123"]
- [wpb_childpages slug="parent-page"]
- 2. The menu's toggle link is disabled *by default*; so to enable it, set the
- menu_toggle parameter to 1, like so:
- [wpb_childpages id="123" menu_toggle="1"]
- 3. The menu is shown/expanded by default; so to hide it by default, set the
- show_menu parameter to 0, like so:
- [wpb_childpages slug="parent-page" menu_toggle="1" show_menu="0"]
- 4. To use a custom label for the menu's toggle link, set the menu_toggle to
- the label that you prefer. Example:
- [wpb_childpages slug="parent-page" menu_toggle="Show/Hide Menu"]
- Note that show_menu is "ignored" if the menu_toggle is 0 (or empty).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement