How to make a custom template in WordPress work as a password protected page?

孤人 提交于 2019-12-22 06:31:15

问题


I'm building a page with a custom template.

The thing is, I need this page to be password protected, or at least accessible to logged in users, but even if I set it as such (Private/Password protected) in the New Pages section in WordPress Administration, it won't display the menu entry nor the content (if Private) or it would show the page contents immediately (if Password protected).

I've read somewhere that the_content() function is what makes this work, but as you can guess, my custom template doesn't use the_content() at all, and it's all based on custom content.

Do you happen to know how can I (re)implement these two options?


回答1:


There's a much more elegant and reliable approach to checking if a post is private;

if ( post_password_required() ) {
    // It's protected and they haven't entered a password, so ask for one:
    the_content();

} else {
    // It's not protected or they have entered a password
}


来源:https://stackoverflow.com/questions/3054245/how-to-make-a-custom-template-in-wordpress-work-as-a-password-protected-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!