0

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

<?php
$args=array(
  'post_parent' => 27641, // This page!
  'post_type' => 'page',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {?>
    <ul>
  <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
                    <?php the_title_attribute(); ?>
                </a>
            </li>
   <?php endwhile; } ?>
   </ul>
<?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
1

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.