当前位置:首页-WordPress文章-WordPress-正文

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的深度级别
本文原创,作者:萨龙龙,其版权均为4166am金沙信心之选所有。
如需转载,请注明出处:/wordpress-get-comment-depth.html
\u6211\u76f8\u4fe1\u60a8\u5e94\u8be5\u77e5\u9053wp_list_comments()\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u8bc4\u8bba\u6253\u5370\u5728\u7f51\u7ad9\u9875\u9762\u4e0a\u3002

\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u529f\u80fd\u5f88\u9177?

1\u3001\u8fd9\u4e2a\u51fd\u6570\u8fd0\u884c\u4e00\u4e2a\u8bc4\u8bba\u5faa\u73af\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5305\u542b\u5faa\u73af\u4e2d\u5f53\u524d\u8bc4\u8bba\u7684\u6df1\u5ea6\u7ea7\u522b\u3002

2\u2026@\u8428\u9f99\u7f51\u7edc&appkey=746468751&pic=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&searchPic=true","qq":"https:\/\/connect.qq.com\/widget\/shareqq\/index.html?url=https:\/\/salongweb.com\/wordpress-get-comment-depth.html&title=WordPress \u4e3b\u9898\u83b7\u5f97\u8bc4\u8bba\u6df1\u5ea6\u76842\u79cd\u65b9\u6cd5&pics=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&summary=\u4e00\u3001\u901a\u8fc7\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u83b7\u53d6\u8bc4\u8bba\u6df1\u5ea6
\u6211\u76f8\u4fe1\u60a8\u5e94\u8be5\u77e5\u9053wp_list_comments()\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u8bc4\u8bba\u6253\u5370\u5728\u7f51\u7ad9\u9875\u9762\u4e0a\u3002

\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u529f\u80fd\u5f88\u9177?

1\u3001\u8fd9\u4e2a\u51fd\u6570\u8fd0\u884c\u4e00\u4e2a\u8bc4\u8bba\u5faa\u73af\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5305\u542b\u5faa\u73af\u4e2d\u5f53\u524d\u8bc4\u8bba\u7684\u6df1\u5ea6\u7ea7\u522b\u3002

2\u2026&site=\u8428\u9f99\u7f51\u7edc","zone":"https:\/\/sns.qzone.qq.com\/cgi-bin\/qzshare\/cgi_qzshare_onekey?url=https:\/\/salongweb.com\/wordpress-get-comment-depth.html&title=WordPress \u4e3b\u9898\u83b7\u5f97\u8bc4\u8bba\u6df1\u5ea6\u76842\u79cd\u65b9\u6cd5&desc=\u4e00\u3001\u901a\u8fc7\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u83b7\u53d6\u8bc4\u8bba\u6df1\u5ea6
\u6211\u76f8\u4fe1\u60a8\u5e94\u8be5\u77e5\u9053wp_list_comments()\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u8bc4\u8bba\u6253\u5370\u5728\u7f51\u7ad9\u9875\u9762\u4e0a\u3002

\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u529f\u80fd\u5f88\u9177?

1\u3001\u8fd9\u4e2a\u51fd\u6570\u8fd0\u884c\u4e00\u4e2a\u8bc4\u8bba\u5faa\u73af\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u5305\u542b\u5faa\u73af\u4e2d\u5f53\u524d\u8bc4\u8bba\u7684\u6df1\u5ea6\u7ea7\u522b\u3002

2\u2026&summary=&site=\u8428\u9f99\u7f51\u7edc"}>
\u6211\u76f8\u4fe1\u60a8\u5e94\u8be5\u77e5\u9053wp_list_comments()\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u8bc4\u8bba\u6253\u5370\u5728\u7f51\u7ad9\u9875\u9762\u4e0a\u3002\u2026","thumb":"https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg","date":{"day":"16","year":"2021\/09"},"desc":"WordPress\u4e3b\u9898|WordPress \u4f01\u4e1a\u4e3b\u9898|WordPress \u5546\u57ce\u4e3b\u9898|\u9ad8\u54c1\u8d28\u7f51\u7ad9\u5b9a\u5236","style":{"light":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-light.png","title":"#242F40","text":"#5B6B82","meta":"#9AA7B9","border":"#EFF5FF","bg":"#fff"},"dark":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-dark.png","title":"#B1C1E8","text":"#b3bdd3","meta":"#565E75","border":"#1C2032","bg":"#171B2C"}}} :loading="posterLoading">