0

I have a site that has Parent and Child pages. I want to be able to show a list of the child pages, listed in Random order. In addition I want to limit the number of child pages shown.

I have found a solution to Listing the pages randomly, however, I need to 1. limit the the number of Child Pages returned
2. Add the Parent Page at the top of the list.

Example:
Parent Page
Child Page
Child Page
Child Page
The Code that I am using is:

add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );
function wpb_list_child_pages() { 
    global $post; 

    if ( is_page() && $post->post_parent ) {
        $childpages = wp_list_pages( 'sort_column=rand&title_li=&child_of=' .  $post->post_parent . '&echo=0' );
    } else {
        $childpages = wp_list_pages( 'sort_column=rand&title_li=&child_of=' .  $post->ID . '&echo=0' );
    }

    if ( $childpages ) {    
        $string = '<ul>' . $childpages . '</ul>';
    }

    return $string;
}

1 Answer 1

2

You can make all sorts of selections using wp_query. In your case try this:

$query = new WP_Query (array( 
  'post_parent' => 93    // get the children of page with ID 93
  'post_count' => 7      // get 7 child pages
  'orderby' => 'rand'    // order the results randomly
   ));

Once you have this, you can loop through $query in the usual way.

2
  • cjbj, thak you for the answer, i found another solution, but need some help in limiting the number of records return. here is the code:
    – JWKlaudi
    Dec 26, 2016 at 15:51
  • add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' ); function wpb_list_child_pages() { global $post; if ( is_page() && $post->post_parent ) { $childpages = wp_list_pages( 'sort_column=rand&title_li=&child_of=' . $post->post_parent . '&echo=0' ); } else { $childpages = wp_list_pages( 'sort_column=rand&title_li=&child_of=' . $post->ID . '&echo=0' ); } if ( $childpages ) { $string = '<ul>' . $childpages . '</ul>'; } return $string; }
    – JWKlaudi
    Dec 26, 2016 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.