How to redirect a Parent Page to a Child Page in WordPress

Q: How do I redirect a parent page to the first subpage in my WordPress navigation?

A: I have had to do this many times on sites with deep content, especially with drop down menus. It’s so easy for a visitor to a site to go directly to the first page in the drop-down menu and completely miss looking at what might be the most important information on the main parent page.

There are a couple different ways to approach this, and many websites and plugins do a 301 redirect which redirects the page to a page of your choosing. However, 301 redirects are typically used for redirects that are meant to be permanent — such as a page on the site that doesn’t exist any more and over time should stop getting hits. These redirects show up in your stats much like 404 page not found stats.

A clean way to deal with parent to first child redirects is to simply set up a page template with the following code and then choose this template as the template for the parent page.

Creating a Custom Page Template to Redirect a WordPress Parent Page to First Child Page

To do this:

1 – Create a new, blank php file in a text editor. Call this file page-gotochild.php.

2 – Copy the following code into the file.

<?php
/*
Template Name: Go to first child
*/
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
} else {
// Do whatever templating you want as a fall-back.
}
?>

3 – Save the file and upload to your active theme’s folder (in the wp-content > themes > nameofyourtheme folder).

4 – Log in to your WordPress Dashboard.

5 – In your Pages list, hover over the parent page you wish to redirect to the first child.

6 – Click Quick Edit and choose Go to first child from the Template list.

Selecting page template in WordPress QuickEdit

7 – Click the Update button.

Now this page will automatically go to the first child page when you view the page. However, the first child may not be the page you want it to redirect to as the first child is the first child alphabetically.

Changing Order of WordPress Pages Using Order Number

wordpress-page-order-default
Default Page Order – Alphabetical

To re-order your child pages,  you can use Quick Edit to assign each page an Order number. This can be tedious if you have a lot of child pages to order. To order a lot of child pages, the Simple Page Ordering Plugin rocks! It allows you to drag pages in your page list to the order you wish.

If you would like to avoid installing yet another plugin, you can manually reorder your pages as follows:

1 – In the Pages list, hover over the page you would like to be the first child page.

2 – Click Quick Edit.

3 – In the Quick Edit window, type 10 for the page order. (Note: I always use increments of 10 for page ordering in case I need to insert a new page between two pages later. This avoids having to renumber all of the pages.)

wordpress-page-order-quickedit-2

4 – Click the Update button.

5 – Repeat for all the child pages using increments of 10.

The new page order will look like this:

New page order with Services parent page redirecting to Our Services
New page order with Services parent page redirecting to Our Services

 

WordPress Page Order and Custom Menus

If you are using Custom Menus (Appearance > Menus), you can certain order the pages as they appear in the menu via the Custom Menu interface, however, this page “order” is not official — it is simply cosmetic for the purposes of displaying items in the menu. Therefore, if a page appears as the first “subpage” on the Custom Menu, it does not mean that it will display a the “first child” according to our little script above. You still need to set the page order in the list of Pages.

Angela Bowman

Front-end WordPress developer since 2007 building highly custom websites for nonprofits and small businesses. Experienced in nonprofit administration, grant writing, and technical writing. Love high altitude hiking and backyard chickens.

29 comments on “How to redirect a Parent Page to a Child Page in WordPress

    • Hi Tigran,

      Yes this method relies on using a page template, so you can choose it on any page of the site, and it will automatically redirect that parent page to the child page.

      I’m not sure what code you are using for your redirection, so I can’t tell you what might not be working with that. page.php is used for all pages on the site, so that’s why it helps to make a template for this.

      Angela

  1. Hi,
    We are converting our site to WordPress and we are working on a beta version to make changes before we go live.

    I have this issue on two Parent pages on our site. One page is a list of press releases. I tried adding numbers to the Order field in the Page Attributes section of the child page but it doesn’t change the order. Every time I add a page and select the parent page it adds it to the bottom of the list of press releases.

    Another page is a list of Articles. Same issue, when I create a new article, it just adds it to the bottom of the list of articles on the parent page.

    I see there are some plugins for reordering posts, but I was not sure if I’m missing something before we try a plugin.

    • Hi Mary,

      For Press Releases and Articles, I recommend using posts instead of pages as pages will get out of control super quick, and you’ll have hundreds of pages in no time which because quite unwieldy to manage. Posts are the best type to use for content that continues to grow. Pages are static, but may create dynamic content, like a list of posts.

      So, I would have your two pages: Press Releases and Articles, BUT don’t create child pages off of these. Instead, create POSTS and put them in a categories called Press Releases and Articles (use a different slug than the Press Releases page). Then using PHP code or a plugin, you can pull these posts onto their respective pages.

      By default, posts are ordered by entry date, so you can use whatever plugin or PHP you are using to populate the page to change the orderby criteria or use the https://wordpress.org/plugins/post-types-order/ plugin to manually order the posts or set the defaults.

      Let me know if you have any questions.

      Angela

    • Yes, I do that, too. It’s a great solution! Often, I’ll make that parent page not have any super important content in case people don’t click on it and make it sort of a landing page with navigation to the child pages. Thanks for mentioning that.

  2. Having trouble getting this to work when visiting my site on a smartphone. The redirect doesn’t work, it just displays the blank page.

  3. This was great and super simple to implement. One question though…Everything works perfectly except on a mobile browser. When i try to navigate via top level item on my iPhone, I get an error saying line 5 of the .php has errors and am not navigated to the child. Has anyone else encountered this or know how to fix?

  4. Hi Angela,

    I’m a newbie when it comes to WordPress sites, and I’ve tried searching for an answer to my question … hence coming across your site 😀

    I have a client site that I have created a sub/child page and when I try to access the parent page, the site automatically skips to the sub/child page (I hope I’m explaining this properly).

    The site is http://www.therealfoodrevolution.com.au/ and the “problem” page is Tips And Tricks.

    I’m assuming I’m missing something fairly obvious, but having looked at this for a couple of hours, I have no idea where to turn next … are you able to help please?

    Thank you

    Cathy

    • Hi Cathy,

      I edited your comment, so now you don’t have to apologize. 🙂

      I see a few things about your navigation to investigate:

      1 – The Home link goes to http://www.therealfoodrevolution.com.au// instead of http://www.therealfoodrevolution.com.au/ so you might want to fix that.

      2 – The main nav is on the home page is set up to go to what is called anchor links on the page, so when you click on the link, it takes you to spot further down the page. Under Appearance > Menus, you will probably see this.

      3 – The Tips and Tricks page itself seems to come up okay: http://www.therealfoodrevolution.com.au/tips-and-tricks/ on its own.

      So, if you don’t want to use the crazy anchor link stuff in the menu, then you might want to modify the menu under Appearance > Menus to actually contain links to the pages rather than links to the anchor tags.

  5. Hi guys, Think I found of another easier way to do this but you may know better why this isn’t right – I’m just learning…

    When you create a Menu in WordPress you can add a link as a Top Level Menu item and put in whatever link you like, so you can add in the link to the first child page there, even though the Link Title is called something else.

    • Yes, this is a good solution, and I do that often or simply use the # symbol for the link to force people to have to pic an item from the drop down menu. However, if Google indexes the page, visitors to the page will go to a blank page. So, you might want to delete the blank page or use the instructions in this post to redirect that page automatically to the first child page.

  6. Hello,

    Thank you so much for posting this. It will save me hours of research, this was on my to do list before I go live with my website.

    Linda

    • Awesome, Linda. And great to see you on Twitter, too. I’m sorry for the late response. For some reason, I didn’t see all these comments. Anyway, I’m glad it helped.

Leave a Reply

Your email address will not be published. Required fields are marked *