• Hi All

    I am using a plugin called Page-list. I use the plugin to list sub-pages of certain pages and sub-pages of the child-pages of other pages. So for the book of Leviticus I have the main page:

    https://gucu.me.uk/the-bible/the-old-testament/leviticus/

    At the bottom of this page I use the short-code
    subpages sort_column=”menu_order, post_date” class=”page-list-cols-3″
    This produces the list at the bottom of that page.

    If you then click on Leviticus 1, I then use this short-code
    pagelist sort_column=”menu_order, post_date” child_of=”512126″ class=”page-list-cols-3″
    to reproduce the same list as the page above.

    However, there are over 1000 chapters in the Bible so I will have to reproduce this code on every page. Is it possible to create if statements to reproduce the same effect in a page template please?

    I found this code doing a web search but it produces errors.

    function Index_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 = ‘
    ‘ . $childpages . ‘
    ‘;
    }
    return $string;
    }
    add_shortcode(‘index_childpages’, ‘Index_child_pages’);

    Any ideas please?

    Thanks

    Rich

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I cleaned up the code above as it has fancy quotes and that might be your error with it. I tested the below code locally and seems to do what you want.

    function Index_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 = '' . $childpages . '';
    	}
    	return $string;
    }
    add_shortcode('index_childpages', 'Index_child_pages');
    Thread Starter Richard Brown

    (@cregy)

    Thanks for the help. I’ve just realised that it is using shortcode! If I want it to auto display without the need to insert a shortcode would this work please?

    //* Add child page list
    function list_child_pages() {
    
        if ( is_page() ) {
            $current_page_id = get_the_ID();
            $child_pages = get_pages( array(
                'child_of' => $current_page_id,
            ) );
    
            if ($child_pages) {
                echo '<div class="row windows_row_holder">';
                foreach ($child_pages as $child_page) {
    
                    $page_id    = $child_page->ID; // get the ID of the childpage
                    $page_link  = get_permalink( $page_id ); // returns the link to childpage
                    $page_title = $child_page->post_title; // returns the title of the child page
    
                    ?>
                <div class="childpage-<?php echo $page_id; ?> col-md-4 pb_30">
                    <div class="inner">
                        <div class="content_txt">
                            <h3><?php echo $page_title; ?></h3>
                        </div>
                    </div>
                </div>
                <?php
                }//END foreach ($child_pages as $child_page)
                echo '</div>';
            }//END if ($child_pages)
        }//END if (is_page())
    };

    Thanks

    Rich

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List Child Pages of Parents’ is closed to new replies.