Modifying get_posts to display children and grandchildren of current page

Hello,
I'm trying to modify my existing loop to include also the sub-sub-pages of my Services page.

<?php 
                global $post;
                $args = array('post_parent' => $post->ID, 'post_type' => 'page', 'posts_per_page' => -1); 
                $custom_posts = get_posts($args);
                foreach($custom_posts as $post) : setup_postdata($post); ?>
                <div class="col-sm-8 col-md-4 service">
                    <div>
                        <header>
                            <h3><?php the_title(); ?></h3>
                        </header>
                        <p><?php the_field('short_description');?></p>
                        <p><a role="button" class="btn btn-primary" href="<?php the_permalink();?>">Learn more</a></p>
                    </div>
                </div>
                <?php endforeach;
                ?>

Open in new window


The problem is that when I change it to get_pages, it doesn't display only pages of my current page, but all pages, and with get_posts I don't know how to add grandchildren in the parameters. Can someone help? Thank you!
cstelaAsked:
Who is Participating?
 
eemitCommented:
>The problem is that when I change it to get_pages, it doesn't display only pages of my current page, but all pages,
Here is an example using get_pages():
global $post;
$mypages = get_pages( array( 'child_of' => $post->ID ) );

// If $post->ID is NULL, you can try the following:
$page = get_page_by_title( 'Title of your service page' );
$pageid = $page->ID;

$mypages = get_pages( array( 'child_of' => $pageid ) );
foreach ( $pages as $page ) {
...

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.