函数官方参考:
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() ?>