如何在WordPress中制作自定义模板作为受密码保护的页面?

And*_*ero 5 php wordpress password-protection

我正在构建一个包含自定义模板的页面.

问题是,我需要这个页面受密码保护,或至少可以登录用户访问,但即使我在WordPress管理的新页面部分中设置它(私人/密码保护),它也不会显示菜单条目或内容(如果是私人)或它将立即显示页面内容(如果密码保护).

我已经读过某个地方,the_content()函数是使这个工作的原因,但正如你猜测的那样,我的自定义模板根本不使用the_content(),而且它都基于自定义内容.

你碰巧知道我怎样才能(重新)实现这两个选项?

The*_*dic 9

有一种更加优雅和可靠的方法来检查帖子是否是私密的;

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
}
Run Code Online (Sandbox Code Playgroud)

  • 这段代码放在哪里?在我们的自定义模板page.php中?还是在哪里? (2认同)