A few of our projects here at Code Greene required the use of WordPress, and those projects had situations that I have since encountered in a site I built in WordPress for someone in my family. That situation was trying to redirect to a parent or child page when a certain page was clicked on.
I know that may seem a bit confusing, but let me give an example. Say I have an “About” page, and that “About” page has three child pages; “Who I Am”, “What I Do”, and “Why I Do It”. Now let’s also say that my “About” page doesn’t have any actual content, it’s just a container for the other three pages, and I just want to redirect my visitors to the “About | Who I Am” page without ever hitting the “About” page. Well, here’s how I do that.
First we need a page template that we can use for the “About” page that will do all the magic for us:
In a new file in your theme folder called redirect_down.php (you can really call it anything you want, this is what I used), put the following:
<?php
/*
Template Name: Redirect Down
*/
// grab the direct children pages of this page
// (both child_of and parent are needed)
$sub_pages = get_pages(array('child_of' => $post->ID, 'parent' => $post->ID, 'sort_column' => 'menu_order', 'number' => 1));
$URI = get_permalink($sub_pages[0]->ID);
// redirect the user down one level in the tree
header('Location: '.$URI);
How this works is it finds the first child of the page you are on, grabs the URI for it, and redirects you to that page.
Now when I create my “About” page (the one with no content), I set my page template to “Redirect Down” in the template drop down, and voilà, when a visitor clicks on the “About” link, they automatically get redirected to the “About | Who I Am” page, no questions asked. If I had pages under the “Who I Am” page, and it was just a container for those, I could also set my template for the “Who I Am” page to “Redirect Down”, and when my visitor clicked the “About” link, it would redirect to the “Who I Am” page, which would then redirect to the first child page.
To complete the collection… in one of the projects I worked on recently, we had a situation where if a child page link was clicked, we needed to redirect up the tree to the parent page. This is basically the same thing, just the other direction, but also a bit easier to code. Here is how it is achieved:
In a new file in your theme folder called redirect_up.php (again, you can call it anything you want, this is just what I used), put the following:
<?php
/*
Template Name: Redirect Up
*/
// redirect the user up one level in the tree
$URI = rtrim($_SERVER['REQUEST_URI'], ' /');
$URI = substr($URI, 0, strrpos($URI, '/') + 1);
if ( ! in_array($URI, array('', '/'))) {
header('Location: '.$URI);
}
The way this works, is it grabs the URI given, and simply removes the last directory from the URI. (You could also find the parent ID from the $post data and use that to find the permalink, much like the previous template, but this works for what I needed it to do.) If you have your permalinks set up properly, this should work beautifully. And again, with this one, you can set it on as many pages as you want, redirecting all the way up the tree if you wish.
Hope this helps someone. Let me know if it helped you in the comments below.