is_user_logged_in与wp_redirect冲突

问题描述 投票:0回答:1

我试图从未登录的用户隐藏我的自定义帖子类型单页。我用钩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一样,它会重定向到登录页面。登录后,如果我尝试访问同一个帖子,它会再次重定向回登录页面,但与其他属性一起正常工作。

它只是在登录之前加载网址:(

php wordpress wordpress-theming custom-wordpress-pages
1个回答
0
投票

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

if ( is_user_logged_in() ) { 
   //your content file 
     get_template_part('content'); 
} 
else {
   //show login page  
     get_template_part('must-login'); 
}
© www.soinside.com 2019 - 2024. All rights reserved.