有没有办法在 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 帖子的样式。
关于wordpress - 样式 WordPress protected 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105822/