create wordpress page template to redirect to first child page

June 9, 2009 · Print This Article

Many WordPress users uses parent pages and subpages to order the informations displayed on their blogs.

Then how to be able to redirect to the first child page if the current page have children pages?

You have to create a wordpress page template to redirect to first child page, Create a new file and paste the following code in it:

 

<?php
/*
Template Name: Redirect To First Child
*/
if (have_posts()) {
  while (have_posts()) {
    the_post();
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));
  }
}
?>

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.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.