只需要WordPress登录帖子,而不是页面?

只需要WordPress登录帖子,而不是页面?,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我正在尝试创建一个带有登录系统的WordPress站点,但我只希望在查看帖子时需要登录。他们应该能够在不登录的情况下查看页面。我做了一些研究,但没有人知道我要找的东西,所以我试着把一些东西拼凑起来,这就是我想到的,但它不起作用 function my_force_login() { global $post; if ( ( is_single() && !is_user_logged_in() ) ){ auth_redirect

我正在尝试创建一个带有登录系统的WordPress站点,但我只希望在查看帖子时需要登录。他们应该能够在不登录的情况下查看页面。我做了一些研究,但没有人知道我要找的东西,所以我试着把一些东西拼凑起来,这就是我想到的,但它不起作用

function my_force_login() {
    global $post;
        if ( ( is_single() && !is_user_logged_in() ) ){ 
            auth_redirect(); 
        }    
    }

有什么想法吗?

将其添加到single.php中

<?php auth_redirect(); ?>


此函数将检查用户是否已登录,如果用户未登录,则会将其重定向到登录页面,该帖子的详细信息将显示在single.php中。请将显示帖子内容的代码放入条件中。然后你就会得到答案。在twentyforteen主题的single.php中尝试以下代码

if ( is_user_logged_in() ) {
   get_template_part( 'content', 'single' );
}

现在注销并刷新浏览器,您将看不到任何内容显示。

您的代码可以正常工作,但它已在上运行:


默认情况下是否将帖子可见性设置为private?但是,您的函数应该可以工作,您如何使用它?请记住,
single.php
被post和自定义post类型使用。如果你只想重定向博客文章,你需要显式地测试它。我认为这个功能很好!非常感谢你!
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(); 
    }    
});