the_post_password()
云策文档标注
概述
the_post_password() 函数用于显示当前文章的密码,通过 esc_attr() 进行转义以确保在 HTML 属性中安全使用。
关键要点
- 函数输出当前文章的密码,适用于需要密码保护的场景
- 使用 esc_attr() 对密码进行转义,防止 XSS 攻击,确保 HTML 属性安全
- 依赖于 get_post() 获取文章数据,需确保文章对象存在
代码示例
function the_post_password() {
$post = get_post();
if ( isset( $post->post_password ) ) {
echo esc_attr( $post->post_password );
}
}注意事项
- 函数在 WordPress 2.7.0 版本中引入,使用时需确保版本兼容性
- 仅当文章设置了密码时才会输出,否则无输出
- 适用于模板文件中直接调用,如显示密码输入提示或相关功能
原文内容
Displays the post password.
Description
The password is passed through esc_attr() to ensure that it is safe for placing in an HTML attribute.
Source
function the_post_password() {
$post = get_post();
if ( isset( $post->post_password ) ) {
echo esc_attr( $post->post_password );
}
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |