4

Let assume I have the following page structure

/about-us (used as a parent holder only)
/about-us/history (real page)
/about-us/team (real page)
/about-us/industry (real page)

There is no content in the page "about-us ", but it is needed for two purpose

  1. Allow hierarchical structure in url
  2. Allow hierarchical structure in menu

But one of the issue is when user type "/about-us" then they will enter a blank page with non content.

So, should I auto forward user to url say "/about-us/history" by default?

Or it is the common way of handling hierarchical pages?

4

I am using two strategies here...

1) is simple redirection to first child (using menu order) page-redirect.php

<?php
/*
 * Template Name: Redirector
 * Description: Empty Holder (redirect page) 
 */


    $rp = new WP_Query(array(
        'post_parent'   => get_the_id(),
        'post_type'     => 'page',
        'order'         => 'asc',
        'orderby'       => 'menu_order'
    ));

    if ($rp->have_posts())
        while ( $rp->have_posts() ) { 
            $rp->the_post(); 
            wp_redirect(get_permalink(get_the_id()));
            exit;
        }   
    wp_redirect(dirname(home_url($wp->request)));
    exit;

2) generating a menu on a parent with a links to children (as example of it - http://unu.edu/about)

1

Create a new file called page-parent.php and enter the following code in your new file:

<?php
/*
 * Template Name: Parent Menu
 * Description: Redirects empty parent page to first child page
 */

# Parent menu goes to first child page
# askwpgirl.com/redirect-parent-page-first-child-page-wordpress
$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 ) );
}

Then, place the page-parent.php in the root directory of your child theme, for example:

/wp-content/themes/child-theme

After that, you will see the new template Parent Menu in the Template option.

Save the parent menu with the new template and whenever tried to access the parent page directly, it will redirect to the first child page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.