I am building a page template within a theme that lists child pages for a product page.

Is it possible to change this so that it lists the children of the page the user is on rather than having to specify a page ID?

My code

  'post_parent' => 27641, // This page!
  'post_type' => 'page',
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {?>
  <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
                    <?php the_title_attribute(); ?>
   <?php endwhile; } ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post().?>
  • You should be able to access the global $post object. – Howdy_McGee Oct 3 '16 at 15:20

The ID of the current page is available via the global $post variable, so to get the ID use:

global $post;
$currentPage = $post->ID;

Then you can use $currentPage in your query :)

  • No probs, glad to help. – prettyfly Oct 4 '16 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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