在wordpress中,我想为登录用户和登出用户设置不同的主页(当有人点击我的网站标题时,简单地更改重定向网址)。有没有办法在我的主题functions.php中添加代码片段?我试过了:
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) {
global $post;
if ($post->ID == 1) {
$permalink = 'http://localhost/homepage-for-logged-users';
}
}
return $permalink;
}
不幸的是它不起作用,当我将网站上传到实时主机时,将功能从localhost更改为我的域名是不合适的。有人可以给我一些建议吗?谢谢!
编辑:我使用ajax"使用ajax"我可以在登录后定义重定向,并将此片段放在我的functions.php。
中// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {
// Check to see if user in not logged in and on the home page for logged users
if(!is_user_logged_in() && is_404()) {
// If user is, Redirect to home page for logged out users.
wp_redirect(home_url('home-logged-out'));
exit;
}
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
*我将已登录用户的页面设为私有,因此已注销的用户将看到404.这不是一个非常干净的方法,但现在可以使用...
答案 0 :(得分:0)
将默认主页设置为用户应该看到的登录页面。
将此代码添加到functions.php:
add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
if( !is_user_logged_in() ) {
wp_redirect( 'http://www.YourSite.com/SomePage');
exit;
}
}
登录用户将看到他们应该看到的内容,未登录的用户将被重定向。