Redirect to first child in menu
I have the following page hierarchy:
- Page Grandparent (This page)
- Page Parent 1 (Should direct here)
- Page Parent 2
- Page Child
- Page Child
- Page Parent 3
- Page Parent 4
My parent and child pages have been assigned via page attributes. The pages are then ordered via Appearance > Menus.
On the front end, when you click on Page Grandparent, I'd like it to redirect to the next menu item (Page Parent 1).
To achieve this, I've built the following page template and applied it to my Page Grandparent.
?php
/**
* Template Name: Redirect to next menu item
*/
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));
exit;
}
}
?
For some reason, the page is directing to Page Parent 2. What am I doing wrong here?
Topic template-redirect wp-redirect Wordpress
Category Web