Would if statement do it:
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
}
that’s brilliant thanks!!!
What would be the best way to incorporate conditional redirection?
For example, if I have a Products page with four sub-pages for each product, I don’t want it to go to Product 1 automatically; I want it to stay on the Products page which has a introductory write-up. At the same time, however, I may have an About Us page which has four sub-pages, but I want the first sub-page to be displayed when the About Us page is visited.
I’d suggest using a custom field called “dont_redirect” which can be set to 0 or 1. It will default to 0 (false), which means if this isn’t specifically set to 1, it will always redirect to the children.
Here is the entire code block modified:
if (have_posts()) {
while (have_posts()) {
the_post();
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
if (!get_post_meta($post->ID, 'dont_redirect', true)) {
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
}
}
}
}
Now I just set a ‘dont_redirect’ custom field in my Products page, set it to 1 and the page stays right there!
Thanks to wildpawtrax for the original code and MichaelH for the infinite loop fix.
What about altering the wp_list_pages?
I started a thread…
http://wordpress.org/support/topic/249009?replies=2
<?php
/*
Template Name: Redirect To First Child
*/
if (have_posts()) {
while (have_posts()) {
the_post();
$pagekids = get_pages(“child_of=”.$post->ID.”&sort_column=menu_order”);
if ($pagekids) {
$firstchild = $pagekids[0];
wp_redirect(get_permalink($firstchild->ID));
}
}
}
?>
Hi there,
I am using this template on a page that I am building, but I am using the qtranslate multi language plugin, so when I click in the parent link I am always redirected to the main language child, even if on an other language. I find out it writes the URL slightly wrong: wordpress/?page_id=212&lang=se
Any ideas how to solve this?
sorry, this is the URL: wordpress/?page_id=212 & a m p ; lang=se
I put michael’s code inside the loop but I got the ‘cannot modify header’ error. I then moved it to the top just before get_header
and it works.
Just my .2 🙂