【问题标题】:Wordpress redirect specific link to custom linkWordpress 将特定链接重定向到自定义链接
【发布时间】:2017-11-19 11:01:28
【问题描述】:

我有一个 wordpress 网站,我想将特定链接重定向到我的自定义链接。我不想使用任何插件,并希望通过在 functions.php 文件中编写代码来实现这一点。我试过的代码写在下面

function custom_redirect() {
    if(is_page(7)) {
        wp_redirect('http://example.com/my-account/orders/', 301);
        exit();
    }
}
add_action ('template_redirect', 'custom_redirect');

现在,我想要重定向用户的页面 ((is_page(7))) 的 url 是 http://www.example.com/my-account/,我想将他们重定向到 http://www.example.com/my-account/orders。我也尝试了site_url('/my-account/') 功能,但无法实现。

【问题讨论】:

  • 使用 is_page(7) 有什么意义,这和我的帐户是同一个页面吗?
  • 是的。 is_page(7) 是'example.com/my-account',我想重定向到'example.com/my-account/orders'。

标签: wordpress redirect


【解决方案1】:

问题是当您测试 is_page('my-account') 时,即使您正在查看 WooCommerce 帐户端点,它也会返回 true。解决方法是检查请求的页面 slug 的全局 $wp 变量。

function custom_redirect() {
    global $wp;

    if( $wp->request == 'my-account' ) {
        wp_redirect( site_url( 'my-account/orders/' ) );
        exit;
    }
}

add_action ('template_redirect', 'custom_redirect');

【讨论】:

    【解决方案2】:

    我不确定我是否理解,但我建议:

    function custom_redirect() {
        if(is_page(7)) {
            header('Location: http://example.com/my-account/orders/');
            exit();
        }
    }
    add_action ('template_redirect', 'custom_redirect');
    

    【讨论】:

    • 使用内置的 WordPress 功能进行重定向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2013-08-14
    相关资源
    最近更新 更多