Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress: click on Parentpage Redirect on first child page programmatically

I want to redirect users on first child page.

for example there is parent page: Page A it has 2 child pages: child1 and child 2

when user click on Page A that redirects user to child 1 page

there are too many redirection plugins to redirect parent to child 1 that is manully set. I want this dynamically

is it possible to redirect the parent page on first child page programmatically?

like image 461
Jack Torris Avatar asked Dec 12 '22 11:12

Jack Torris


2 Answers

The following worked like a charm. (http://www.wprecipes.com/wordpress-page-template-to-redirect-to-first-child-page)

To achieve this recipe, you have to create a page template. Create a new file and paste the following code in it:

<?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));
    exit;
  }
}
?>

Save the file under the name redirect.php and upload it to the wp-content/themes/your-theme directory of your WordPress install. Once done, you can use the page template.

like image 186
Max Stern Avatar answered May 02 '23 22:05

Max Stern


Try below code for the same :

$pageChilds = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pageChilds) {
$firstchild = $pageChilds[0];
wp_redirect(get_permalink($firstchild->ID));
}

Let me know if you have any queries!

Thanks.

like image 23
Chandresh M Avatar answered May 02 '23 22:05

Chandresh M