使用 wp_redirect 重定向 wordpress 页面 [英] using wp_redirect to redirect wordpress pages

查看:44
本文介绍了使用 wp_redirect 重定向 wordpress 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我不想使用其他插件来做简单的重定向任务,所以我决定使用以下代码.

Since I don't want to use another plugin to do simple redirect tasks, I decided to use the following code.

wp_redirect( "http://www.example.com/contact-us",301);

wp_redirect( "http://www.example.com/contact-us", 301 );

所以这是我的问题.

假设我有一个名为https://www.example.com/contact-us-3/" 需要重定向到 "https://www.example.com/contact-us/".

Let's say I have a page called "https://www.example.com/contact-us-3/" that needs to redirect to "https://www.example.com/contact-us/".

或者我应该说我真的希望重定向发生,而不管 http 或 https、www 还是非 www.我希望/contact-us-3/"重定向到/contact-us/"页面.

Or I should say I really want the redirect to happen regardless of http or https, www or non-www. I am want "/contact-us-3/" to redirect to "/contact-us/" page.

这是否意味着我必须将以下代码放入 wordpress 内容中?我到底把代码放在哪里?子主题中的function.php?我是否指定需要重定向的页面?或者我必须创建一个页面/contact-us-3/"并将代码放在页面中?

Does that mean I have to put the following code inside the wordpress contents? Where do I exactly put the code? function.php in the child theme? I do I specify the page that needs to be redirected? or I do have to create a page "/contact-us-3/" and put the code in the page?

另外,我是否必须输入完全限定的域名 url?

Also, do I have to put the fully qualified domain name url?

推荐答案

您可能希望将重定向代码放入绑定到 template_redirect 钩子的回调函数中.在您的主题的functions.php 文件中放置类似于以下的代码.名为some_condition"的函数(您编写的)将确定页面是否应该被重定向.

You may want to put your redirect code into a callback function that is tied to the template_redirect hook. Put code similar to the following in the functions.php file of your theme. The function named "some_condition" (which you write) will determine if the page should be redirected or not.

add_action( 'template_redirect', 'my_callback' );
function my_callback() {
  if ( some_condition() ) {
    wp_redirect( "http://www.example.com/contact-us", 301 );
    exit();
  }
}

这篇关于使用 wp_redirect 重定向 wordpress 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆