昨天有客户问我,如何实现计算wordpress的评论数目。小编不精编程,所以一时语言塞。通过论坛交流,获得如下代码,可以实现。如果你的wordpress主题不自带自动计算评论数功能的话,你可以参考如下代码,希望能帮你解决问题。
1、 写出帖子ID。
[comments id=”1″]
2、 下面的这段源代码,就可以帮助我们实现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 . ‘”>’ . $cc . ‘</a>’;
}
add_shortcode(‘comments’, ‘comments_shortcode’);
纯Linux环境、免备案【海外简单云】php空间,108元一年起。
【网上创业步骤】:
1 开通云主机、域名 2 建设官方网站 3 整体网站推广 … 成功见证 创业技能素质培训
更多有价值的内容:
简单主机官方网店 store.simplehost2.com