WordPress 主题获得评论深度的2种方法
一、通过使用全局变量获取评论深度
我相信您应该知道wp_list_comments()
函数,它可以将评论打印在网站页面上。
为什么这个功能很酷?
1、这个函数运行一个评论循环,并设置一个全局变量,该变量包含循环中当前评论的深度级别。
2、wp_list_comments()
允许您通过使用回调参数指定自己的评论模板。
- wp_list_comments('callback=my_custom_comment_template');
在my_custom_comment_template()
中,将设置全局变量$comment_depth
和$GLOBALS['comment_depth']
。
二、通过评论ID获取评论深度
如果全局变量$comment_depth
不可用,我怎么得到一个评论深度级别呢?
我想这个简单的函数可以帮助你:
- function salong_get_comment_depth($my_comment_id) {
- $depth_level=0;
- while($my_comment_id > 0) {
- $my_comment=get_comment($my_comment_id);
- $my_comment_id=$my_comment->comment_parent;
- $depth_level++;
- }
- return $depth_level;
- }
然后:
- echo salong_get_comment_depth(784); // 打印出评论ID 为784的深度级别
"愿我的文字能带给您一丝美好"
分享海报
下载海报

162021/09
WordPress 主题获得评论深度的2种方法
一、通过使用全局变量获取评论深度<br>我相信您应该知道wp_list_comments()函数,它可以将评论打印在网站页面上。…
萨龙网络
WordPress主题|WordPress 企业主题|WordPress 商城主题|高品质网站定制