is_user_logged_in conflict with wp_redirect



我正在尝试对未登录的用户隐藏我的自定义帖子类型单页。我按如下方式使用了钩子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;      
}

上面的代码有效,但存在一些问题。就像我尝试访问它重定向到登录页面 http://example.com/property/abc 一样。登录后,如果我尝试访问同一帖子,它会再次重定向回登录页面,但是与其他属性配合使用正常。

它只是在再次登录之前加载 url :(

如果它只是您尝试隐藏的单个帖子,则可以将以下内容添加到sinlge-property.php模板文件中:

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

最新更新