wordpress:以编程方式单击第一个子页面上的父页面重定向

Jac*_*ris 3 wordpress wordpress-theming

我想在第一个子页面上重定向用户.

例如,有父页面:页面A它有2个子页面:child1和child 2

当用户点击页面A时,将用户重定向到子页面1

有太多的重定向插件将父级重定向到man 1设置的manrely.我动态地想要这个

是否可以通过编程方式在第一个子页面上重定向父页面?

Max*_*ern 5

以下工作就像一个魅力.(http://www.wprecipes.com/wordpress-page-template-to-redirect-to-first-child-page)

要实现此配方,您必须创建页面模板.创建一个新文件并将以下代码粘贴到其中:

<?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;
  }
}
?>
Run Code Online (Sandbox Code Playgroud)

将文件保存在名称redirect.php下,并将其上传到WordPress安装的wp-content/themes/your-theme目录.完成后,您可以使用页面模板.