admin管理员组

文章数量:1125966

I want private posts (for those not logged in) to use the single.php template just as public posts do, at the moment they get thrown over to the 404.php template.

I've had a good look through the core files but I cannot find where the template change (note: this is not a redirect) happens.

Can anyone help?

Edit

To explain why I want this, I want all visitors to see a preview of the content, but logged in "members" to see the entire post content.

I want private posts (for those not logged in) to use the single.php template just as public posts do, at the moment they get thrown over to the 404.php template.

I've had a good look through the core files but I cannot find where the template change (note: this is not a redirect) happens.

Can anyone help?

Edit

To explain why I want this, I want all visitors to see a preview of the content, but logged in "members" to see the entire post content.

Share Improve this question edited Jun 5, 2013 at 14:54 Ben Everard asked Jun 5, 2013 at 14:07 Ben EverardBen Everard 1,2785 gold badges19 silver badges33 bronze badges 2
  • Have you done the usual rigamarole (disable plugins, switch to default theme) to see if the 404 theme load is coming from somewhere other than WP core? – Pat J Commented Jun 5, 2013 at 14:33
  • I don't have any plugins installed, this is default functionality. I just want to disable this default functionality so private posts (for those who aren't logged in) shows the single.php template. – Ben Everard Commented Jun 5, 2013 at 14:37
Add a comment  | 

3 Answers 3

Reset to default 2

According to the Content Visibility page, Private posts are visible only to those with sufficient permission levels. WordPress doesn't expose them to non-logged-in users at all, hence (presumably) the 404.

If you want to display a limited preview to non-members, instead of setting the post to Private, you could try something like this:

add_filter( 'the_content', 'wpse101968_preview' );
function wpse101968_preview( $content ) {
    if( is_single() ) {
        if( ! is_user_logged_in() ) {
            $content = get_the_excerpt();
        }
    }
    return $content;
}

And then your non-logged-in visitors will see the post's excerpt instead of the entire post. You can customize the excerpts, too, so that they see only what you want them to see.

References

Codex:

  • is_user_logged_in()
  • get_the_excerpt()

You are not seeing a redirect, to confirm your own observation, so much as you are seeing just what it looks like-- a "File Not Found". Take a look at the query in the source around line 2438 (among others). The status of the post is part of the query itself, so if the status doesn't match then no posts are found and that means 404. As you can see from the code, status is pretty rigidly protected. That is proving difficult to override.

I'd recommend using a custom post type for you "logged in only" posts. That is going to be much easier to control both on the front end and the back end.

How do you set password protected posts to be viewed by logged-out users? We created the functionality and all logged-out users are getting a 404 after inserting the password.

本文标签: templatesPrevent private post 404