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

时间:2013-10-01 10:14:33

标签: wordpress wordpress-theming

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

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

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

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

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

3 个答案:

答案 0 :(得分: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;
  }
}
?>

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

答案 1 :(得分:3)

尝试以下代码:

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

如果您有任何疑问,请与我们联系!

感谢。

答案 2 :(得分:2)

在这种情况下,我只需添加指向菜单的链接,并将网址更改为“父/子”enter image description here

相关问题