Display Related Child Pages in Wordpress

1.1k views Asked by At

I'm developing a website in which child pages have a section displaying randomly its related pages which are its own siblings. They all have the same parent page.

Further explaining:

  • Parent page

    • Child page (shows related pages - its siblings)
    • Child page (shows related pages - its siblings)
    • Child page (shows related pages - its siblings)
    • Child page (shows related pages - its siblings)

Also, this display must be random, so that each page display will show a different range of related pages.

I'm imagining there might be a way to extend the wp_list_pages() in such a manner to display siblings. What I use in the parent page to display all children is:

<?php
    wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->ID
    ));
?>

I need this because I don't want to install a plugin to add category to pages so that I can use an existing related posts/page plugin. Another solution would be the existence of a plugin that allows related pages to be siblings (I didn't find one... most use taxonomy).

1

There are 1 answers

0
Paulo Gabriel On BEST ANSWER

Here's what I needed:

<?php
    $args = array(
        'post_type' => 'page',
        'post_parent' => $post->post_parent,
        'post__not_in' => array($post->ID),
        'post_count' => 5,
        'orderby' => 'rand'
    );

    $the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>" rel="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php the_title(); ?></a>
            </li>
    <?php endwhile; ?>
<?php endif; ?>