How to redirect a parent page to the first child element?

In WordPress, the pages can already be created in a hierarchy, which can help the clarity but also the URL structure. However, if the parent web page should not be filled with content and link directly to the child element, a little coding is required.

Solution: Ideally, you create an empty php file (e.g. blank.php) in a child theme, so that this file is not overwritten when the theme is updated. Finally, insert the following code.

 * Template Name: Parent Menu
 * Description: Redirects empty parent page to first child page

$child_page = get_pages( "child_of=" . $post->ID . "&sort_column=menu_order" );
if ( $child_page ) {
    $parent_page = $child_page[0];
    wp_redirect( get_permalink( $parent_page->ID ) );

In the last step, the template must be changed in the backend on the desired parent page. In the above case, on “Parent Menu”.

Without cookies
This website does not use cookies or tracking. More information can be found in the privacy policy.