简体   繁体   English

is_user_logged_in与wp_redirect冲突

[英]is_user_logged_in conflict with wp_redirect

I am trying to hide my custom post type single page from non-logged in users. 我试图对未登录的用户隐藏我的自定义帖子类型单页。 I used hook template_redirect as follows: 我使用钩子template_redirect如下:

add_action('template_redirect','hide_single_property');
function hide_single_property()
{
    if( is_singular('property') || is_page('dashboard')):
        if( ! is_user_logged_in() ):
            wp_redirect(get_permalink(103),302);
            exit;
        endif;
    endif;      
}

the code above works, but with some problem. 上面的代码有效,但存在一些问题。 Like i try to visit http://example.com/property/abc it redirects to login page. 就像我尝试访问http://example.com/property/abc一样,它会重定向到登录页面。 And after login if i try to visit the same post it again redirects back to login page, however works fine with other properties. 登录后,如果我尝试访问相同的帖子,它将再次重定向回登录页面,但是可以与其他属性一起正常使用。

It just loads the url before login again :( 它只是在再次登录之前加载URL :(

if its just the single post you're trying to hide, you can add the following to your sinlge-property.php template file: 如果仅是您要隐藏的单个帖子,则可以将以下内容添加到sinlge-property.php模板文件中:

if ( is_user_logged_in() ) { 
   //your content file 
     get_template_part('content'); 
} 
else {
   //show login page  
     get_template_part('must-login'); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM