|  | 
 
| 初识这个函数是因为做的一个企业主题需要添加一个自定义的留言表单,在使用的过程中遇见个不是问题的问题,就是每次打开有留言表单的页面,只出现一个填写评论内容的,默认的姓名、电子邮件和网址都没出现,刚开始还以为自己用错了,最后发现只要退出管理员账号,留言表单就能正常显示姓名、电子邮件、网址和评论所有表了,也是醉了。 
 在 WordPress 主题中, 使用 comment_form()函数来生成一个评论表单。通常把评论模块的代码写进单独的 comments.php 文件中,然后使用 comments_template ()函数在 single.php、page.php 等文件底部引用评论模块。
 
 语法结构
 
 
 复制代码<?php comment_form( $args, $post_id ); ?>
参数
 
 
 $args (array) (optional) 包括$fields、标题和发送等的信息
 $fields
 
 $fields(array) (optional) 控制表单信息,包括
 姓名 – author
 邮箱 – email
 网址 – url
 留言内容 – comment_field
 comment_notes_before – 在评论表单前面显示提示信息
 comment_notes_after – 在评论表单后面显示提示信息
 
 $args
 
 title_reply – 改变评论表单标题,默认是:Leave a Reply。
 title_reply_to
 comment_notes_before – 在评论表单前面显示提示信息
 comment_notes_after – 在评论表单后面显示提示信息
 title_reply_before 给评论表单标题加上HTML结构或text,比如加个<h3>开头
 title_reply_after 给评论表单标题加上HTML结构或text,比如加个</h3>结尾
 cancel_reply_before
 cancel_reply_after
 cancel_reply_link
 must_log_in
 logged_in_as
 comment_field 为false时默认的内容表单将不显示,默认是true
 id_form 控制HTML结构中<form>的id值,默认是commentform
 class_form 控制HTML结构中<form>的class值,默认是comment-form
 id_submit 控制HTML结构中<input type=”submit”>的id值,默认是submit
 class_submit 控制HTML结构中<input type=”submit”>的class值,默认是submit
 label_submit – 这个参数改变评论表单提交按钮文字,默认是:Post Comment
更多详细请看
 
 TIPS
 
 要注意的是,如果你的主题是要给别人用的,特别是外国人,为了国际化,修改的内容要用 __() 这个函数包裹,可以方便翻译
 
 实例
 
 
 复制代码<?php
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array(
    'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . '</p>',
    'author' => '<p><input id="author" placeholder="Name*" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="40"' . $aria_req . ' /></p>',
    'email'  => '<p><input id="email" placeholder="Email*" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="40"' . $aria_req . ' /></p>',
    'comment_field' => '<p></label><textarea id="comment" placeholder="Message*" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
);
$args = array(
    'fields' =>  $fields,
    'title_reply'=>'Contact Form',
    'label_submit' => 'Send Message',
    'comment_field' => false,
    'comment_notes_before' => false
);
comment_form($args);
 实例
 
 在有表单的页面之间放入 <?php comments_template(); ?> 就可引入comments.php文件
 
 在某些情况下,你希望以不同的方式来显示你的评论,这时可以建立一个自定义的文件(例如 other-comments.php),并且通过下面的方式调用:
 
 复制代码<?php comments_template( '/other-comments.php' ); ?>
 | 
 |