wordpress:以编程方式单击第一个子页面上的父页面重定向 [英] wordpress: click on Parentpage Redirect on first child page programmatically
问题描述
我想在第一个子页面上重定向用户.
I want to redirect users on first child page.
例如有父页面:页面 A 它有 2 个子页面:child1 和 child 2
for example there is parent page: Page A it has 2 child pages: child1 and child 2
当用户点击页面 A 将用户重定向到子页面 1
when user click on Page A that redirects user to child 1 page
有太多的重定向插件无法将父级重定向到手动设置的子级 1.我想要这个动态
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?
推荐答案
以下内容非常有效.(http://www.wprecipes.com/wordpress-page-template-to-redirect-to-first-child-page)
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;
}
}
?>
以redirect.php 名称保存文件并将其上传到WordPress 安装的wp-content/themes/your-theme 目录.完成后,您可以使用页面模板.
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.
这篇关于wordpress:以编程方式单击第一个子页面上的父页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!