函數官方參考:
https://codex.wordpress.org/Function_Reference/have_comments
開發評論模板時遇到一個問題,當博文有評論時,一切正常,

當博文無評論時,評論框不顯示。

原因:
是判斷函數:have_comments()的問題
我在代碼中是這樣寫的:
if ( have_comments() ) : ?>
?
????
???? comment_form(array(
????
'comment_notes_after' => '
請訪問我們的 隱私政策 頁面。
',
????????'title_reply'=>'添加新評論',
????//評論輸入框上方文字
????????'comment_notes_before'=>'
' . __( '' ) . ( $req ? $required_text : '' ) . '
',
????//評論輸入框
???? ???'comment_field' => '
' . _x( '', 'noun' ) . '
',
????
????
????????'label_submit'=>__( '提交評論' ),
????//強制登陸留言
????????'must_log_in'=>'
' . sprintf( __( '您必須e登陸 才能留言.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '
',
????//登陸為
????????'logged_in_as'=>'
' . sprintf( __( '當前身份為:%2$s,您需要退出嗎?' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '
',
));
????
?>
????
????<h2 class="comments-title">
printf( _nx( '一個想法 "%2$s"', '"%2$s"共有 %1$s 條評論 ', get_comments_number(), '注釋標題', 'mirages' ),
number_format_i18n( get_comments_number() ), '' . get_the_title() . '' );
?>h2><ol class="comment-list">
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 120,//頭像大小
????????????????????'reply_text' =>'回復',/*回復文字*/
????????????????????'callback' => 'mirages_comment'/*使用自定義回調函數控制評論的外觀*/
) );
?>ol>
????
????<div class="pagination"> paginate_comments_links(); ?>
div>
????
????
// Are there comments to navigate through?
????????????//是否有可瀏覽的注釋?if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
?> endif; // Check for comment navigation(檢查注釋導航) ?>
????
if ( ! comments_open() && get_comments_number() ) : ?><p class="no-comments"> _e( '評論已關閉.' , 'mirages' ); ?>p> endif; ?>
endif; // have_comments() ?>
?
當博文中無評論時,函數判斷為假,函數判斷中的內容不顯示。
當博文中有評論時,函數判斷為真,顯示判斷中的內容。
在此處,這個函數是用來控制評論中的博文標題是否顯示的,而我卻在其中把評論框也包進去了。
應該是這樣寫:
if ( have_comments() ) : ?>
<h2 class="comments-title">
printf( _nx( '一個想法 "%2$s"', '"%2$s"共有 %1$s 條評論 ', get_comments_number(), '注釋標題', 'mirages' ),
number_format_i18n( get_comments_number() ), '' . get_the_title() . '' );
?>h2>
endif; // end have_comments() ?>