输出完整的自定义备注表单

时间:2015-06-18 作者:vajrasar

需要以十二条注释的形式实现以下引导布局-

<div class="sub-figure">
    <div class="sub-post-title">
        <h4 class="recent-title">Leave A Comment</h4>
    </div>
    <form class="form-inline">
        <div class="col-xs-12 col-sm-4 col-md-4 form-group">
            <input type="text" name="author" id="author"  class="form-control height-in" id="reply-name" placeholder="Enter Name">
        </div>
        <div class="col-xs-12 col-sm-4 col-md-4 form-group">
            <input type="email" name="email" id="email" class="form-control height-in" id="replay-email" placeholder="Enter Email">
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 form-group top-equal">
            <textarea name="comment" id="comment" class="form-control" rows="7" placeholder="Comment"></textarea>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 form-group top-equal">
            <input name="comment_post_ID" value="<?php echo $post->ID; ?>" type="hidden">
            <input type="submit" name="submit" id="submit" class="btn btn-1 btn-1a ct-us-send">
        </div>
    </form>
</div>  
我读过codex,也研究过comment\\u form(),但无法完全重新定义注释表单布局。

此外,在许多情况下,客户端需要注释表单中的一些自定义字段,我想知道是否可以重新定义comment\\u form(),然后可能还有一种方法可以引入新字段,将其值保存在db中,并像其他默认字段一样检索其值?

有什么想法吗?

1 个回复
最合适的回答,由SO网友:vajrasar 整理而成

以下是HTML–

<!-- Comment Form HTML -->
<div class="sub-figure">
    <div class="sub-post-title">
        <h4 class="recent-title">Comments</h4>
    </div>
</div>  

<!-- Leave a comment -->
<div class="sub-figure">
    <div class="sub-post-title">
        <h4 class="recent-title">Leave A Comment</h4>
    </div>
    <form class="form-inline">
      <div class="col-xs-12 col-sm-4 col-md-4 form-group">
        <input type="text" class="form-control height-in" id="reply-name" placeholder="Enter Name">
      </div>
      <div class="col-xs-12 col-sm-4 col-md-4 form-group">
        <input type="email" class="form-control height-in" id="replay-email" placeholder="Enter Email">
      </div>
      <div class="col-xs-12 col-sm-12 col-md-12 form-group top-equal">
        <textarea class="form-control" rows="7" placeholder="Comment"></textarea>
      </div>
      <div class="col-xs-12 col-sm-12 col-md-12 form-group top-equal">
        <input type="submit" class="btn btn-1 btn-1a ct-us-send">
      </div>
    </form>
</div>  

<!-- Comments List HTML -->
<div class="comment-details">
    <div class="comment-pic">
        <img src="img/comment.jpg">
    </div>
    <div class="comment-inner">
        <div class="comment-info">
            <div class="clearfix">
                <h4>Vajrasar</h4>
                <a href="#"><i class="fa fa-reply"></i> Reply</a>
            </div>
            <div class="comment-on">
                <h6>September 9, 2014</h6>
            </div>
            <div class="comment-text">
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
            </div>
        </div>
    </div>
</div>
以下是我用来输出修改后的注释表单的代码–

<?php

//Disabling URL field
function vg_disable_comment_url( $fields ) { 
    unset( $fields[\'url\'] );
    return $fields;
}
add_filter(\'comment_form_default_fields\',\'vg_disable_comment_url\');

//To edit defaults in comment field
function vg_modify_comment_defaults( $defaults ) {
    $defaults[\'comment_notes_after\'] = \'\';
    $defaults[\'comment_notes_before\'] = \'\';
    $defaults[\'title_reply\'] = \'Leave A Comment\';
    $defaults[\'comment_field\'] = \'\';

    return $defaults;
}
add_filter( \'comment_form_defaults\', \'vg_modify_comment_defaults\' );

//To modify structure of input fields and the wrapping html markup
function vg_modify_comment_form_fields( $fields ){

    $fields[\'author\'] = \'<div class="col-xs-12 col-sm-4 col-md-4 form-group">\' 
                . \'<input class="form-control height-in" placeholder="Enter Name" id="author" name="author" type="text" value="\' 
                . esc_attr( $commenter[\'comment_author\'] ) 
                . \'" size="30"\' 
                . $aria_req 
                . \' /></div>\';

    $fields[\'email\']  = \'<div class="col-xs-12 col-sm-4 col-md-4 form-group">\' 
                . \'<input class="form-control height-in" placeholder="Enter Email" id="email" name="email" \' 
                . ( $html5 ? \'type="email"\' : \'type="text"\' ) 
                . \' value="\' 
                . esc_attr(  $commenter[\'comment_author_email\'] ) 
                . \'" size="30"\' 
                . $aria_req 
                . \' /></div>\';

    $fields[\'comment_field\'] = \'<div class="col-xs-12 col-sm-12 col-md-12 form-group top-equal">
                        <textarea class="form-control" placeholder="Comment" id="comment" name="comment" cols="45" rows="7" aria-required="true"></textarea>
                        </div>\';    

    $fields[\'comment_notes_after\'] = \'<input type="submit" class="btn btn-1 btn-1a ct-us-send" id="submit-new" value="\' . __( \'Submit\' ) . \'" />\';

    return $fields;
}

add_filter(\'comment_form_default_fields\',\'vg_modify_comment_form_fields\');
以下内容将取代默认注释列表调用注释。WP本机主题文件中的php-

wp_list_comments( \'type=comment&callback=vg_format_comment\' ); //vg_format_comment is the function being called
以下是输出修改注释列表的代码–

//To modify comment list and its wrapping markup using vg_format_comment function defined in callback
function vg_format_comment( $comment, $args, $depth ) {
    $GLOBALS[\'comment\'] = $comment;
    $ca = get_comment_author();
    ?>
    <div class="comment-details">
        <div class="comment-pic">
            <?php echo get_avatar( $comment, 32 ); ?>
        </div>
        <div class="comment-inner">
            <div class="comment-info">
                <div class="clearfix">
                    <h4><?php printf(__(\'%s\'), get_comment_author_link()) ?></h4>
                    <div id="reply-cover">
                        <i class="fa fa-reply"></i> <?php comment_reply_link(array_merge( $args, array(\'depth\' => $depth, \'max_depth\' => $args[\'max_depth\']))) ?>
                    </div>
                </div>
                <div class="comment-on">
                    <h6><?php printf(__(\'%1$s\'), get_comment_date(), get_comment_time()) ?></h6>
                </div>
                <div class="comment-text"> 
                    <?php comment_text(); ?>
                </div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
<?php 
}
以下是我用来为WP输出的注释表单提供自定义类的内容-

// To add custom class in comment form in comment.php in wp native themes
ob_start();
comment_form();
echo str_replace(\'class="comment-form"\',\'class="form-inline"\',ob_get_clean()); //class added is form-inline

结束

相关推荐

“Comments.php”在可湿性粉剂管理中编辑后不可用

在主题“Point”中,“comments.php”页面没有正确翻译。我想在WP管理编辑器中手动翻译标签(法语),但是when I update the file, the comments doesn\'t appear anymore on my website.然而,我只是改变了标签。知道我做错了什么吗?此问题是否与主题或我的WP安装相关?Here is the code before: <?php // Do not delete these lines if