我正在尝试使用登录系统创建一个WordPress网站,但我只想要登录才能查看POSTS。他们应该能够在没有登录的情况下查看页面。我做了一些研究,但是没有人确切地想要我想要的东西,所以我试图拼凑一些东西,这就是我提出的,但它不起作用。
function my_force_login() {
global $post;
if ( ( is_single() && !is_user_logged_in() ) ){
auth_redirect();
}
}
有什么想法吗?
答案 0 :(得分:2)
将其添加到您的single.php
<?php auth_redirect(); ?>
此功能将检查用户是否已登录,如果不是,则会将其重定向到登录页面http://codex.wordpress.org/Function_Reference/auth_redirect
答案 1 :(得分:1)
您的代码有效,但它已在correct point上运行:
add_action( 'template_redirect', function()
{
global $post; // Use this to detect the custom post types if needed
if ( ( is_single() && !is_user_logged_in() ) )
{
auth_redirect();
}
});
答案 2 :(得分:0)
该帖子的详细信息显示在single.php中。请将显示帖子内容的代码放入条件中。然后你会得到解决方案。请尝试将以下代码放入二十二主题中的single.php中。
if ( is_user_logged_in() ) {
get_template_part( 'content', 'single' );
}
现在退出并刷新浏览器,您可以看到没有显示任何内容。