【问题标题】:Style wordpress protected posts样式 wordpress 保护的帖子
【发布时间】:2015-01-23 08:31:22
【问题描述】:

有没有办法在 Wordpress 中设置受密码保护的帖子的样式?另外,我似乎只能保护而不是除此之外的任何自定义字段。

编辑 – 尽可能避免使用插件

【问题讨论】:

    标签: wordpress passwords


    【解决方案1】:

    我假设您的意思是在前端设置受密码保护的帖子的样式。

    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;
    }
    

    希望这将帮助您设置受保护帖子的样式。

    【讨论】:

    • 似乎无法使函数正常工作,出现以下错误: Parse error: syntax error, unexpected T_FUNCTION in .../wp-content/themes/2015/functions.php 在线4
    • 我假设你的 PHP >= 5.3 ... 让我为旧版本更新它... 请立即查看
    • 它到达那里。没有在帖子页面上收到错误,但在另一个页面上收到此错误:警告:无法修改标头信息 - 标头已发送(输出开始于 .../beta/wp-content/themes/2015/functions.php:3 ) 在 /.../beta/wp-includes/pluggable.php 第 1178 行
    • 我猜你在functions.php文件中添加了一些空行,尝试删除它们并在编辑之前备份文件。
    • 啊太棒了!没有意识到它对额外的线条如此敏感。现在一切正常,感谢您的帮助:)
    【解决方案2】:

    您可以使用 Protected Post Personalizer 插件来解决问题https://wordpress.org/plugins/protected-post-personalizer/

    【讨论】:

    • 谢谢,抱歉,我尽量避免使用插件。
    • 它到达那里。没有在帖子页面上收到错误,但在另一个页面上收到此错误:警告:无法修改标头信息 - 标头已发送(输出开始于 .../beta/wp-content/themes/2015/functions.php:3 ) 在 /.../beta/wp-includes/pluggable.php 第 1178 行
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多