如何使 WordPress 中的自定义模板用作受密码保护的页面? [英] How to make a custom template in WordPress work as a password protected page?

查看:71
本文介绍了如何使 WordPress 中的自定义模板用作受密码保护的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义模板构建页面.

I'm building a page with a custom template.

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

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).

我在某处读到过 the_content() 函数使这项工作起作用,但您可以猜到,我的自定义模板根本不使用 the_content(),它完全基于自定义内容.

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?

推荐答案

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

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
}

这篇关于如何使 WordPress 中的自定义模板用作受密码保护的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆