Home > Back-end >  Retrieve newest Child page from specified Parent page - Wordpress
Retrieve newest Child page from specified Parent page - Wordpress

Time:11-28

I'm having trouble retrieving the newest Child Page from a specified Parent page.

So far I've managed to muddle together this:

    <?php
        $childArgs = array(
            'sort_order' => 'ASC',
            'sort_column' => 'post_date',
            'child_of' => '408', // Page ID: Shop 
        );

        $childList = get_pages($childArgs);
        foreach ($childList as $child) { ?>



          <p>
              <?php echo get_the_excerpt($child)?> //Insert and style retrieved content here
          </p>
            

    <?php } ?>

But this returns all the Children pages not one Child (which is the newest page).

I tried replacing the foreach loop with a for loop counting up to 1 page but that didn't work.

Could anyone recommend a good solution?

CodePudding user response:

You should try using only first element of array.

<?php
$childArgs = array(
            'sort_order' => 'ASC',
            'sort_column' => 'post_date',
            'child_of' => '408', // Page ID: Shop 
        );
$childList = get_pages($childArgs);
if(!empty($childList)) { ?>
      <p>
          <?php echo get_the_excerpt($childList[0])?>
      </p>
<?php } ?>
  • Related