登录用户和注销用户的首页不同,但WordPress中的URL相同

时间:2021-04-23 作者:Andre Dike

我有两个在WP创建的主页。其中一个设置为首页,但仅针对已注销的用户。对于登录的用户,他们将主页2视为自己的主页。

但是,我希望登录用户的URL是我网站的默认URL。因此,单纯的重定向并不能像我将要做的那样奏效;地点com/home-2;。

我有没有办法做到这一点?下面的代码不起作用,破坏了我的网站。

unction switch_homepage() {
    if ( is_user_logged_in() ) {
        $page = 2516; // for logged in users
        update_option( \'page_on_front\', $page );
        update_option( \'show_on_front\', \'page\' );
    } else {
        $page = 2; // for logged out users
        update_option( \'page_on_front\', $page );
        update_option( \'show_on_front\', \'page\' );
    }
}
add_action( \'init\', \'switch_homepage\' );

?>

1 个回复
SO网友:Pat J

如果您只是想向登录用户和注销用户显示不同的内容,我建议使用the_content 滤器

add_filter( \'the_content\', \'wpse_387084_content_selector\' );
function wpse_387084_content_selector( $content ) {
    if ( is_front_page() && is_user_logged_in() ) {
        $page = get_post( 2516 );
        $content = $page->post_content;
    }
    return $content;
}
以上代码假定您已设置;“首页”;到ID为的页面2, 这就是您想要向未经身份验证的用户显示的内容。