Post Pic

WordPress page template to redirect to first child page

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:

<?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.
If you don't know how to use page templates, refer to this recipe.

Related Posts

Related Posts

No related posts.

25 Responses

Jun 08 2009 09:08

WoW man! Great work. Thnaks for this useful post.

Jun 08 2009 11:55

Great one!!!!

Thanks

Jun 08 2009 12:14

Thank you! Exactly what I’ve been looking for

Jun 08 2009 12:21

absolutely great Work :)

Jun 08 2009 13:43

its a nice one, its usefull when using WP as CMS

Jun 08 2009 13:57

Funny you post this, spent a few hours today looking for something to do just this!

Jun 08 2009 15:56

Nice, simple solution — and very handy. Thanks.

Jun 08 2009 16:44

Very useful tutorial if you don’t want to have content on parent page, but you rather want to redirect all visits to first child page. Thanks for sharing.

Jun 08 2009 16:51

Good tip. I certainly see the need for something like this all the time when using multiple levels in the page hierarchy. I wonder if it would make more sense to redirect the first child back up to the parent instead? Imagine a non-profit organization web site with a page called “Membership” with something like the following children:

Membership
– General Information
– Top 10 Reasons to Join
– Find a Member
– Manage Membership

I agree that the “Membership” page and the “General Information” page should be the same, but I think the URL should be that of the parent …

/membership/

… instead of the child …

/membership/information/

I have used the “Page Links To” plugin for this in the past, but I think using a page template like “Redirect to Parent Page” in line with this article might be even better for that. What do you think?

Jun 08 2009 17:58

Just came across your Blog today looking for a solution to have a page template redirect to first to child page. It’s funny that you just wrote about this today.

Jun 08 2009 18:57

If you globalize the $post variable, you can make the code a little more efficient.


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

Also, you could use a plugin that I wrote that does the same thing without a page template:
http://www.nathanrice.net/plugins/

Jun 08 2009 19:24

I just wanna say one word “”"”awesome”"”".
Keep on buddy.

Jun 09 2009 18:36

Thanks for this very useful tip. I had been using a drop down navigation on several WordPress sites, and I was always a bit stuck on what to do with the main parent page. Traditionally I just stuffed it with the links to the children pages from the drop down, but with this technique that cumbersome solution is now a thing of the past.

Thanks again.

Jun 09 2009 21:50

I wrote a perfect plugin for that a while ago.
With a nice clean GUI and advanced settings for each page.

Check it out here: http://wordpress.org/extend/plugins/redirector/

Jun 10 2009 04:52

@Horttcore: Great plugin. Thanks for that.

Jun 25 2009 20:39

Thanks! You have no idea how much hassle you just saved me. Great post.

Jul 16 2009 04:02

Great post. I was searching around for a good solution and was thinking that just listing the children was the best idea. But automatically redirecting is even better.

One thought. It looks like wp_redirect does a 302 temporary redirect by default. If you do wp_redirect(get_permalink($firstChild->ID), 301) instead it will send a 301 permanent redirect which I would imagine is preferable. At least it is in my case.

Aug 18 2009 13:11

Nice peace of code! Thank you!

I found a new – and for me smarter way – to redirect pages.

Here is the code that I have placed in my page.php:

<?php
global $post; // ID, ‘redirect www:’, true);
// use custom field for input ID
$redirect_field2 = get_post_meta($post->ID, ‘redirect id);
if($redirect_field1) wp_redirect(clean_url($redirect_field1), 301);

// get permalink
if($redirect_field2) wp_redirect(get_permalink($redirect_field2), 301);

get_header();
?>

Have fun!

Aug 18 2009 13:15

Oh,
the code was broken after posting it here.

here again:

// this code must be placed before get_header();

global $post; // ID, ‘redirect www:’, true);
$redirect_field2 = get_post_meta($post->ID, ‘redirect ID:’, true);
if($redirect_field1) wp_redirect(clean_url($redirect_field1), 301);
// works with permalink
if($redirect_field2) wp_redirect(get_permalink($redirect_field2), 301);

get_header();

Sep 09 2009 20:43

I get an endless loop.

Dec 05 2009 17:44

Oh my God, you just saved my whole weekend!!!

Love you man!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required