WordPress.org

Ready to get started?Download WordPress

Forums

Redirecting Pages (5 posts)

  1. jugularbean
    Member
    Posted 6 years ago #

    I have 5 pages on my WP install, and each of the pages has 4-6 child pages.

    On clicking a link for one of the pages I want it to redirect to its first child.

    I'm using this code to find the first child link
    $subPage = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_parent = " . $pageID . " ORDER BY menu_order");

    I thought I'd try using wp_redirect($subPage)
    but it's not working

    Is there any better way of doing this? I'm sure I'm overlooking something simple

  2. mores
    Member
    Posted 6 years ago #

    hmmm, maybe replace
    wp_redirect($subPage) with header("Location:{$subPage}");

    does your code work with permalinks too?
    and what about pages that have no subpages?

    and most important: where do i put your code? because i need this feature as well !

    this is more elegant than a plugin, but if you're stuck try this one
    http://urbangiraffe.com/plugins/redirection/

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    If I wanted to redirect pages to external URLs, I'd use http://redalt.com/Resources/Plugins/Redirectify .

    However, given that you want to redirect to another page (the first child), I'd use this:

    $pagekids = get_pages("child_of=$pageID");
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));

    You might need to use $firstchild['ID'] instead, I'm uncertain there.

  4. adrianghe
    Member
    Posted 6 years ago #

    which is the correct synthax? i've tried the last one but doesnt' work..

  5. rsgca
    Member
    Posted 5 years ago #

    In ver 2.6.2, I'm using the following php code to perform this redirect:

    // redirect from empty category pages to first child
    if ( is_page( array('page-slug1', 'page-slug2')) ) {
       $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
       $firstchild = $pagekids[0];
       wp_redirect(get_permalink($firstchild->ID));
    }

    Note that this code block should be placed at the top of your header.php file.

Topic Closed

This topic has been closed to new replies.

About this Topic