小赖子的英国生活和资讯

双评论升级版本: 评论多的时候才启用

阅读 桌面完整版

坛子的这篇博客 介绍了简单加入双评论框的代码.好处就是当评论多的时候方便读者找到评论框留言. 如果只有一两条,就没必要显示两个框. 其实可以调用 wordpress 里的内置函数 get_comments_number() 来获得当前帖子的评论数目.非常方便.当然,可以指定参数 post_id 来获得具体哪篇博文的评论数目.

这样,只需要在 模板文件 comments.php 里的 <?php if ( have_comments() ) : ?> 后加上

1
2
3
4
5
<?php
  if (get_comments_number() >= 5) { // 大于五篇才显示双评论框
     comment_form();
  }
?>
<?php
  if (get_comments_number() >= 5) { // 大于五篇才显示双评论框
     comment_form();
  }
?>

真是那句话: 细节改进无止境.

另: comment_form 所带的 tag 是 #respond, 如果两个框,那么就是两个 <a name=’respond’> 或者 <div id=’respond’> 在引用 页面 #respond 的时候会自动跳到第一个评论框里. 有强迫症的请绕行.

英文同步: https://helloacm.com/how-to-add-another-comment-form-when-the-comment-count-is-large-for-wordpress-posts/

强烈推荐

微信公众号: 小赖子的英国生活和资讯 JustYYUK

阅读 桌面完整版
Exit mobile version