初識這個函數(shù)是因為做的一個企業(yè)主題需要添加一個自定義的留言表單,在使用的過程中遇見個不是問題的問題,就是每次打開有留言表單的頁面,只出現(xiàn)一個填寫評論內(nèi)容的,默認的姓名、電子郵件和網(wǎng)址都沒出現(xiàn),剛開始還以為自己用錯了,最后發(fā)現(xiàn)只要退出管理員賬號,留言表單就能正常顯示姓名、電子郵件、網(wǎng)址和評論所有表了,也是醉了。
在 WordPress 主題中, 使用 comment_form()函數(shù)來生成一個評論表單。通常把評論模塊的代碼寫進單獨的 comments.php 文件中,然后使用 comments_template ()函數(shù)在 single.php、page.php 等文件底部引用評論模塊。
語法結(jié)構(gòu)
<?php comment_form( $args, $post_id ); ?>
參數(shù)
$args (array) (optional) 包括$fields、標題和發(fā)送等的信息
$fields
$fields(array) (optional) 控制表單信息,包括
姓名 – author
郵箱 – email
網(wǎng)址 – url
留言內(nèi)容 – 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結(jié)構(gòu)或text,比如加個<h3>開頭
title_reply_after 給評論表單標題加上HTML結(jié)構(gòu)或text,比如加個</h3>結(jié)尾
cancel_reply_before
cancel_reply_after
cancel_reply_link
must_log_in
logged_in_as
comment_field 為false時默認的內(nèi)容表單將不顯示,默認是true
id_form 控制HTML結(jié)構(gòu)中<form>的id值,默認是commentform
class_form 控制HTML結(jié)構(gòu)中<form>的class值,默認是comment-form
id_submit 控制HTML結(jié)構(gòu)中<input type=”submit”>的id值,默認是submit
class_submit 控制HTML結(jié)構(gòu)中<input type=”submit”>的class值,默認是submit
label_submit – 這個參數(shù)改變評論表單提交按鈕文字,默認是:Post Comment
更多詳細請看
TIPS
要注意的是,如果你的主題是要給別人用的,特別是外國人,為了國際化,修改的內(nèi)容要用 __() 這個函數(shù)包裹,可以方便翻譯
實例
<?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),并且通過下面的方式調(diào)用:
<?php comments_template( '/other-comments.php' ); ?>