Create a child page dropdown list – WordPress

Create child page dropdown list - WordPress

Over the years WordPress has developed into a very powerful CMS and there’s some very clever stuff being created by some very clever people.

Meanwhile, back at the ranch … A recent client asked to create a dropdown list of child pages and a short while later we had this solution.

Firstly, What is a Child Page?
From the codex:
“To turn your current Page into a SubPage, or “Child” of the “Parent” Page, select the appropriate Page from the drop-down menu. If you specify a Parent other than “Main Page (no parent)” from the list, the Page you are now editing will be made a Child of that selected Page. When your Pages are listed, the Child Page will be nested under the Parent Page.”

The code below can be used within a WordPress theme page to create a dropdown containing a dynamic list of child pages.

The main element to consider is:
‘post_parent’ => 10 <– This is the ID of the parent page.

You can see this in action on this client site.

<?php $args=array(
‘post_parent’ => 10,
‘post_type’ => ‘page’,
‘orderby’ => ‘menu_order’,
‘order’ => ‘ASC’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1,
‘caller_get_posts’=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {?>
<form name=”jump”>
<select onchange=”window.open(this.options[this.selectedIndex].value,’_top’)”>
<option value=”#” selected=”selected”>Please select</option>
<?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>
<option value=”<?php the_permalink() ?>”><?php the_title(); ?></option>
<?php endwhile; } ?> </select>
</form>
<?php wp_reset_query(); ?>

If the above has totally confused you, don’t worry, just get in touch and we’ll sort it for you.

Thank you for sharing: Share this article on Facebook LinkedIn Tweet this