4

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

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

当用户单击将用户重定向到子 1 页面的页面 A 时

重定向插件太多,无法将父级重定向到手动设置的子级 1。我想要这个动态

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

4

3 回答 3

7

以下工作就像一个魅力。(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;
  }
}
?>

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

于 2014-01-27T10:21:06.323 回答
4

尝试下面的代码相同:

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

如果您有任何疑问,请告诉我!

谢谢。

于 2013-10-01T10:30:15.577 回答
2

在这种情况下,我只需添加一个指向菜单的链接,并将 URL 更改为“父/子”在此处输入图像描述

于 2015-06-06T11:57:33.357 回答