給WordPress評論框添加背景文字教程
本文圍繞“給WordPress評論框添加背景文字教程”整理操作說明與配置思路。界面名稱可能隨軟件版本調整,修改線上網站前請保存備份。涉及使用體驗的描述屬於來源作者,本頁未提供獨立實測結論。 因爲最近換上了Hummingbird插件來緩存WordPress文章,而Hummingbird的緩存更新機制有

基本資訊
- 資料範圍
- 完整文章正文,已清理外鏈和來源推廣
- 正文插圖
- 1個真實URL;1張已本地化,其餘使用原圖待補
本文圍繞“給WordPress評論框添加背景文字教程”整理操作說明與配置思路。界面名稱可能隨軟件版本調整,修改線上網站前請保存備份。涉及使用體驗的描述屬於來源作者,本頁未提供獨立實測結論。
因爲最近換上了Hummingbird插件來緩存WordPress文章,而Hummingbird的緩存更新機制有點不科學,緩存更新的話需要全部清空了重新生成。而當有人評論後,頁面不選擇更新緩存的話,就不會顯示出來,所以就研究上了WordPress評論框添加提示文字。
效果如下:

實現方法:
網上搜索到的給WordPress添加評論背景提示文字的方法有兩種方法,大多數的文章都是使用的方法一實現,不過最近幾年,尤其到了了,基本上主題文件都不是以前那麼寫的了,所以大多數都是通過方法二實現。看你主題是哪種方式實現的。
方法一
如果你的主題comments.php 文件裏面可以直接找到下面這串代碼類似的
<textarea name="comment" id="comment" cols="60" rows="10" tabindex="4"></textarea>
那麼直接在這串代碼裏面添加就可以了。
<textarea style="color:gray" onblur="if (this.value == ”) {this.value = ‘我堅信,評論可以一針見血!’;} " onfocus="if (this.value == ‘我堅信,評論可以一針見血!’) {this.value = ”;}" name="comment" id="comment" cols="60" rows="10" tabindex="4">我堅信,評論可以一針見血!</textarea>
方法二
如果你的主題評論文件裏面看不到上面這串<textarea name=”comment”的代碼,那麼就找下面這串代碼
<?php comment_form(); ?>
然後把上面的代碼替換成下面這串代碼就可以實現效果了。
<?php comment_form(
array(
'comment_field' => '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required" placeholder="評論需審覈,評論有緩存…"></textarea>',
)
); ?>
cols=”45″ rows=”8″ maxlength=”65525″這串代碼,你可以查看你主題默認前臺顯示的是哪種,然後替換成主題的代碼。
默認評論區域代碼
$fields = array(
'author' =>
'<p class="comment-form-author"><label for="author">' . __( 'Name', 'domainreference' ) .
( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
'" size="30"' . $aria_req . ' /></p>',
'email' =>
'<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ) .
( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) .
'" size="30"' . $aria_req . ' /></p>',
'url' =>
'<p class="comment-form-url"><label for="url">' . __( 'Website', 'domainreference' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
'" size="30" /></p>',
);
從4.9.6開始,如果“顯示評論cookie選擇加入複選框”。在“討論設置”中選中,cookie同意複選框將添加到區域中。
$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">' . __( 'Save my name, email, and website in this browser for the next time I comment.' ) . '</label></p>';
注意:要在自定義回調函數中使用上述代碼中的變量,必須首先使用以下內容在回調中設置這些變量:
$commenter = wp_get_current_commenter(); $req = get_option( 'require_name_email' ); $aria_req = ( $req ? " aria-required='true'" : '' ); $consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
本文參照了以下文章:
WordPress評論框添加指引說明
自定義評論表單comment_form()
WordPress官方文檔Function Reference/comment form
本站整理公開產品資料與建站知識,介紹不代表實際使用測試。功能、價格、授權和支援範圍請在購買前向提供方確認。 閱讀推廣披露






SHARE YOUR QUESTIONS
交流與討論
分享你的問題和經驗。請勿釋出廣告、敏感資訊或未經核實的評價。