自定义注释字段在登录时不显示

时间:2015-12-21 作者:Valex

我在注释中添加了一个自定义字段,使用

function comment_label($fields){

    unset( $fields[\'url\'] );

    $fields[\'rate\'] = \'<p class="comment_form_rate" id="rate_recipe">
                         <span class="star_rating">

    <input class="star star-5" id="star-5" type="radio"  name="rate" value="5"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="rate" value="4"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="rate" value="3"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="rate" value="2"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="rate" value="1"/>
    <label class="star star-1" for="star-1"></label>     </span>
                        </p>\';

    return $fields;
}
如果您要发布一条状态为未标记的评论,那么它将按预期工作。

但是,一旦您登录,这个字段就会消失,只剩下文本区域输入的注释和所需的title_reply, 即使您不是以管理员身份登录,而是以普通用户身份登录。这是我的comments.php 文件

<?php

if ( post_password_required() ): ?>
     <p class="nopassword"><?php _e( \'Questo articolo è protetto da password. Inserisci la password per visualizzare i commenti.\', \'wtd\' ); ?></p>
<?php endif; ?>

<div id="comments" class="comments-area">

    <?php if ( have_comments() ) : ?>
        <h2 class="comments-title">
            <?php
                printf( _nx( \'Un commento su &ldquo;%2$s&rdquo;\', \'%1$s commenti su &ldquo;%2$s&rdquo;\', get_comments_number(), \'comments title\', \'wtd\' ),
                    number_format_i18n( get_comments_number() ), \'<span>\' . get_the_title() . \'</span>\' );
            ?>
        </h2>

        <ol class="commentlist">
            <?php wp_list_comments( array( \'callback\' => \'wtd_comment\', 
                                              \'style\' => \'p\', 
                                               \'type\' => \'comment\',
                                               \'per_page\' => 8 ) 

                                ); ?>
        </ol><!-- .commentlist -->

        <?php if ( get_comment_pages_count() > 1 && get_option( \'page_comments\' ) ) : // navigazione dei commenti ?>
        <nav id="comment-nav-below" class="navigation" role="navigation">
            <h1 class="assistive-text section-heading"><?php _e( \'Paginazione commenti\', \'wtd\' ); ?></h1>
            <div class="nav-previous"><?php previous_comments_link( __( \'&larr; Commenti Precedenti\', \'wtd\' ) ); ?></div>
            <div class="nav-next"><?php next_comments_link( __( \'Nuovi commenti &rarr;\', \'wtd\' ) ); ?></div>
        </nav>
        <?php endif; // check for comment navigation ?>

        <?php
        /* If there are no comments and comments are closed, let\'s leave a note.
         * But we only want the note on posts and pages that had comments in the first place.
         */
        if ( ! comments_open() && get_comments_number() ) : ?>
        <p class="nocomments"><?php _e( \'I commenti sono chiusi\' , \'wtd\' ); ?></p>
        <?php endif; ?>

    <?php endif; // have_comments() ?>

    <?php 

    //argomenti da stampare in front-end
        $argz = array(
            \'class_form\'           => \'form-control\',
            \'comment_notes_before\' => __(\'il tuo feedback è importante per noi!\',\'wtd\'),
            \'label_submit\'         => __(\'Commenta\',\'wtd\'),
            \'comment_fields\'       => \'\',
            \'title_reply\'          => __(\'Valuta questa ricetta\',\'wtd\'),
        );

    comment_form($argz); ?>

</div><!-- #comments .comments-area -->
我错过什么了吗?

3 个回复
SO网友:birgire

我不确定我是否完全理解您的问题,但要添加一些对登录用户和注销用户都可见的HTML(仅在提交字段上方或下方),您可以尝试以下操作:

add_filter( \'comment_form_submit_field\', function( $submit_field )
{
    //-----------------------------------
    // Adjust the prepend to your needs
    //-----------------------------------
    $prepend = \'<p> Prepend some HTML </p>\';

    //-----------------------------------
    // Adjust the append to your needs
    //-----------------------------------
    $append = \'<p> Append some HTML </p>\';

    return $prepend . $submit_field . $append;
} );
还可以采取其他解决方法,如comment_form 行动

有一些评论表单筛选器仅对注销用户可用,如

  • comment_form_before_fields
  • comment_form_field_{$name}
  • comment_form_after_fields
SO网友:Valex

我是这样管理的,正如您所说,我使用了一个操作挂钩,因为我的过滤器只针对未登录的用户。

function comment_custom(){

    ?><p class="comment_form_rate" id="rate_recipe"><label>Vota la ricetta</label><br/>
                     <span class="star_rating">

<input class="star star-5" id="star-5" type="radio"  name="rate" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="rate" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="rate" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="rate" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="rate" value="1"/>
<label class="star star-1" for="star-1"></label>     </span>
                    </p>
}

    //Those filter makes my ""custom comment field"" both logged and unlogged  
    add_action(\'comment_form_after_fields\',\'comment_custom\');
    add_action(\'comment_form_logged_in_after\',\'comment_custom\');

SO网友:Bibek Shahi Newa

你可能做过

$defaults[\'comment_notes_before\'] = __( \'\' );  
$defaults[\'submit_button\'] = __( \'\' );  
$defaults[\'comment_field\'] = __( \'\' );  
$defaults[\'title_reply\'] = __( \'Leave your Reply\' );  
$defaults[\'label_submit\'] = __( \'Submit Comment\', \'custom\' );  
对于自定义注释表单

现在,Wordpress将显示登录用户的默认注释表单,您已将其初始化为空,因此不会显示任何内容。

解决方案是,我们需要更改注释表单的$默认参数,因为wordpress将显示只有注释字段的默认表单。我们可以通过使用条件

if (!is_user_logged_in()) {  
//custom form  
}  
else{  
//make changes in default comment form  
}  

相关推荐

Get_Users Orderby Page

我一直在尝试使用codex修复排序问题get_users(), 但我似乎无法让它工作。抄本显示我可以设置\'orderby\' => \'post_count\', 但我想更改它,使其按页数排序。看起来应该是很容易改变的(page_count?), 但它不起作用。有人能给我一些建议吗?