【发布时间】:2015-01-23 08:31:22
【问题描述】:
有没有办法在 Wordpress 中设置受密码保护的帖子的样式?另外,我似乎只能保护而不是除此之外的任何自定义字段。
编辑 – 尽可能避免使用插件
【问题讨论】:
有没有办法在 Wordpress 中设置受密码保护的帖子的样式?另外,我似乎只能保护而不是除此之外的任何自定义字段。
编辑 – 尽可能避免使用插件
【问题讨论】:
我假设您的意思是在前端设置受密码保护的帖子的样式。
a) CSS:
如果您的主题使用post_class() 函数,例如:
<article <?php post_class(); ?>>...</article>
然后它会生成:
<article class="... post-password-required ...">...</article>
用于受密码保护的帖子。
所以你可以简单地定位这些帖子:
.post-password-required {
background-color: #eee;
}
在您的样式表中。
b) 形式:
i)如果您想在密码表单中添加文本,您可以使用以下内容:
add_action( 'the_password_form', 'rob_the_password_form' );
function rob_the_password_form( $output )
{
$before = ' Before '; // Modify this to your needs!
$after = ' After '; // Modify this to your needs!
return $before . $output . $after;
}
ii) 如果你想直接修改 HTML 表单,你可以覆盖它:
add_filter( 'the_password_form', 'rob_override_the_password_form' );
function rob_override_the_password_form( $form = '' ) {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$form = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
' . __( "To view this protected post, enter the password below:" ) . '
<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
</form>
';
return $form;
}
这是基于Codex 中的示例。它使用与默认表单相同的表单。
c) 标题:
要更改默认的 Protected: 标题前缀,您可以使用:
add_filter( 'protected_title_format', 'rob_protected_title_format' );
function rob_protected_title_format( $format )
{
$format = __( ' Members only! %s ' ); // Modify this to your needs!
return $format;
}
希望这将帮助您设置受保护帖子的样式。
【讨论】:
您可以使用 Protected Post Personalizer 插件来解决问题https://wordpress.org/plugins/protected-post-personalizer/
【讨论】: