## 一、通过使用全局变量获取评论深度

我相信您应该知道`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的深度级别
```