WordPress page template to redirect to first child page

June 8, 2009 at 8:58 am

Many WordPress users uses parent pages and subpages to order the informations displayed on their blogs. A heavily requested recipe is how to be able to redirect to the first child page if the current page have children pages? here is the answer.

To achieve this recipe, you have to create a page template. Create a new file and paste the following code in it:

Template Name: Redirect To First Child
if (have_posts()) {
  while (have_posts()) {
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $firstchild = $pagekids[0];

Save the file under the name redirect.php and upload it to the wp-content/themes/your-theme directory of your WordPress install. Once done, you can use the page template.
If you don’t know how to use page templates, refer to this recipe.