enter image description here

So this is my code. This piece of code shows me the child page titles. But i'm providing the ID from the parent page. Is there a way to make this dynamic? I don't want to use the ID cuz then its static..

  <?php
                  $childArgs = array(
                      'sort_order' => 'ASC',
                      'sort_column' => 'menu_order',
                      'child_of' => 127
                  );
                  $childList = get_pages($childArgs);
                  foreach ($childList as $child) { ?>

        <ul class="menu-items menu-level-1 menu-count-5">
          <li class="menu-item item-number-1  item-number-2 item-number-3 item-number-4 item-number-5 item-id-84283 item-odd item-page item-node item-alias-over-ons-de-winkel"><a href=""><?php echo $child->post_title; ?></a></li>
        <?php } ?>
New contributor
Gago is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Can you clarify on what exactly you want to achieve? Do you need to show all child pages? Child pages of a specific page? On the page you are browsing? – Pim Oct 11 at 12:06
  • Hi, I have an About page as Parent page. And some child pages linked to the parent page. So on that about page i'm currently echo'ing the title. But lets say I make a new page and make it a parent then that won't work because im using ID from the first parent page (about). U see the problem? So how can i make this dynamic? Thanks for helping me.. @Pim – Gago Oct 11 at 12:11
  • If you're on the page that you want to retrieve children of: Use get_the_ID() instead of 127 – admcfajn Oct 13 at 16:21
up vote 0 down vote accepted

//The correct code

functions.php

  function get_page_parent_id( $id ) {
$args = array(
  'sort_order' => 'ASC',
  'sort_column' => 'menu_order',
  'child_of' => $id
);
$args = get_pages($args);

if(is_array($pages))
  $pageID = $id;
else {
  $pageID = wp_get_post_parent_id( $id );
}

return $pageID;


}
 ?>

page.php

<?php
          $parentID = get_page_parent_id(get_the_ID());

            $childArgs = array(
            'sort_order' => 'ASC',
            'sort_column' => 'menu_order',
            'child_of' => $parentID
          ); ?>
    <div class="subnav">
      <h3 class="subnav-headline"><a href="/over-ons" class="c-dark"><?php echo get_the_title($parentID); ?></a></h3>

        <ul class="menu-items menu-level-1 menu-count-5">
             <?php $pages = get_pages($childArgs);
                foreach($pages as $page ) { ?>
          <li class="menu-item item-number-2 item-id-84286 item-even item-page item-node item-alias-over-ons-geschiedenis-leonidas"><a href="<?php echo get_the_permalink($page);?>"><?php echo $page->post_title;?></a></li>
            <?php   }   ?>
      </ul>
    </div>
New contributor
Gago is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Try the following:

<?php

  global $post;

  $page_id = get_the_id();

  $childArgs = array(
    'sort_order' => 'ASC',
    'sort_column' => 'menu_order',
    'child_of' => $page_id
  );
  $childList = get_pages($childArgs);

  if($childList && (!$post->post_parent || is_page(127))){ ?>
    <ul class="menu-items menu-level-1 menu-count-5">
      <?php foreach ($childList as $child) { ?>
        <li class="menu-item item-number-1  item-number-2 item-number-3 item-number-4 item-number-5 item-id-84283 item-odd item-page item-node item-alias-over-ons-de-winkel">
          <a href=""><?php echo $child->post_title; ?></a>
        </li>
      <?php } ?>
    </ul>
  <?php } ?>

Your Answer

Gago is a new contributor. Be nice, and check out our Code of Conduct.
 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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