If page has subpages – redirect to first child, else – stay on current page
-
Hi everyone,
I am trying to create a new page template so that if the page has subpages, it will automatically be redirected to the first one, and if it doesn’t have any subpages it should display the current page.
I got this code from another forum:
<?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"); $firstchild = $pagekids[0]; wp_redirect(get_permalink($firstchild->ID)); } } ?>
I have tried to alter the code to remain on the current page if there are no children but I cannot stop it going in an infinite redirect loop.
Any suggestions?
Thanks a million,
Pete
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘If page has subpages – redirect to first child, else – stay on current page’ is closed to new replies.