给 WordPress 博客评论添加计数统计代码

前端之家收集整理的这篇文章主要介绍了给 WordPress 博客评论添加计数统计代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
wordpress 博客评论添加计数统计代码,下面这段代码可以很轻易地计算出评论数,不过貌似好多主题自带这个功能了,没带这个功能的朋友就参考做一下吧

首先把帖子ID写出

[comments id="1"]

然后使用下面这段代码就可以实现计数ID1帖子的评论

function comments_shortcode($atts) {
extract( shortcode_atts( array(
'id' => ''
),$atts ) );

$num = 0;
$post_id = $id;
$queried_post = get_post($post_id);
$cc = $queried_post->comment_count;
if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments';
else : $cc = $cc.' Comment';
endif;
$permalink = get_permalink($post_id);

return '<a href="'. $permalink . '" class="comments_link">' . $cc . '</a>';

}
add_shortcode('comments','comments_shortcode');

猜你在找的wordpress相关文章