jugularbean
Member
Posted 2 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
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/
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.
adrianghe
Member
Posted 1 year ago #
which is the correct synthax? i've tried the last one but doesnt' work..
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.