Wordpress将特定链接重定向到自定义链接

时间:2017-11-19 11:01:28

标签: wordpress redirect

我有一个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)))的网址为http://www.example.com/my-account/,我想将它们重定向到http://www.example.com/my-account/orders。我也尝试了site_url('/my-account/')功能,但无法实现。

2 个答案:

答案 0 :(得分:0)

问题是,当您测试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');

答案 1 :(得分:0)

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

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