代码实现类似大前端DUX WordPress主题评论列表显示楼层
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

逛其他童鞋的WordPress网站的时候,发现很多童鞋的评论列表后面的楼层显示第几层。这个功能是怎么实现的了?
比如大前端DUX主题的评论列表就有楼层计数功能。可以到大前端主题文件中看一下人家的写法,毕竟是国内的大佬,值得学习。
该文件位于大前端DUX主题目录下的modules文件夹中,名字叫做mo_comments_list.php:。
function mo_comments_list($comment, $args, $depth) { $GLOBALS['comment'] = $comment; global $commentcount, $wpdb, $post; if(!$commentcount) { //初始化楼层计数器 $page = get_query_var('cpage');//获取当前评论列表页码 $cpp = get_option('comments_per_page');//获取每页评论显示数量 $pcs = get_option('page_comments');//分页开关 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent"); $cnt = count($comments);//获取主评论总数量 if ( get_option('comment_order') === 'desc' ) { //倒序 if (!$pcs || ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) { $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数 } else { $commentcount = $cpp * $page + 1; } }else{ //顺序 if( !$pcs ){ $commentcount = 0; }else{ $page = $page-1; $commentcount = $cpp * $page; } } } echo '<li '; comment_class(); echo ' id="comment-'.get_comment_ID().'">'; if(!$parent_id = $comment->comment_parent ) { echo '<span class="comt-f">#'. (get_option('comment_order') === 'desc'?--$commentcount:++$commentcount) .'</span>'; } echo '<div class="comt-avatar">'; echo _get_the_avatar($user_id=$comment->user_id, $user_email=$comment->comment_author_email); echo '</div>';echo '<div class="comt-main" id="div-comment-'.get_comment_ID().'">';comment_text();if ($comment->comment_approved == '0'){ echo '<span class="comt-approved">待审核</span>'; } echo '<div class="comt-meta"><span class="comt-author">'.get_comment_author_link().'</span>'; echo _get_time_ago($comment->comment_date); if ($comment->comment_approved !== '0'){$replyText = get_comment_reply_link( array_merge( $args, array('add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); // echo str_replace(' h', ' h="javascript:;" data-h', $replyText ); if( strstr($replyText, 'reply-login') ){ echo preg_replace('# class="[\s\S]*?" h="[\s\S]*?"#', ' class="signin-loader" h="javascript:;"', $replyText ); }else{ echo preg_replace('# h=[\s\S]*? onclick=#', ' h="javascript:;" onclick=', $replyText ); } }echo '</div>';echo '</div>';} 内容 1 方法一 2 方法二 2.1 相关文章方法一
像大前端DUX主题一样,在主题目录下建立modules文件夹,然后将上面的代码保存在该文件夹中,再利用如下代码加载到WordPress中。
function _moloader($name = '', $apply = true) { if (!function_exists($name)) { include get_stylesheet_directory() . '/modules/' . $name . '.php'; } if ($apply && function_exists($name)) { $name(); }}加载方式多种多样,直接使用include引用也是可以的。
然后通过_moloader(‘mo_comments_list’, false);来加载上面的代码,最后就是使用了。WordPress提供了评论列表加载方法wp_list_comments函数,该函数可以携带回调函数callback。
wp_list_comments('type=comment&callback=mo_comments_list');上述代码的意思是取出评论数据,并交给回调函数mo_comments_list来处理,mo_comments_list就是上面我们的楼层计数方法,其中包含了输出评论的内容。
方法二
直接将上面的代码写在functions.php文件中,返回在文章页适当位置使用下面代码来调用。
wp_list_comments('type=comment&callback=mo_comments_list');第二个方法无疑是最为简单。但是如果在functions.php中,加入太多的东西难免会显得臃肿。。
[WordPress]历史优惠活动内容
猜你可能想看的VPS
- 疯狂猜成语 图猜成语一个老头和进化论的图片是什么成语?全球[VPS测评]
- 今日秒杀→1C2G1M 年付 88 元三年 268 元 2C4G3M 三香港VPS[主机]
- GbpsCloud→1620 元 月 2GB 内存 30GB 空间 不限虚拟空间(主机)
- Linode-最优秀的免备案 VPS 推荐!全球[VPS测评]
- hostsolutions→€76 年 罗马尼亚大硬盘 VPS 无视版权全球[VPS测评]
- EUserv→德国独服 1Gbps 带宽 不限流量 免费 20 个 IP全球[VPS测评]
- 便宜域名 Name→US 等 5 种域名 1.99 美元 年 CO 等 全球[VPS测评]
- Ikoula→€15 月 E3-1220v5 8GB 内存 1TB 或 全球[VPS测评]
- 推荐 腾讯云秒杀→新增企业 1 折专区 2 核 4G 5M 带宽 3 年全球[VPS测评]
- 活动 磐石云→福州 100G 高防包 福州 BGP 高防 ip 50G 全球[VPS测评]
- 靠谱商家 flipperhost→$99 年 洛杉矶高配 VPS KV全球[VPS测评]
- TheStack→$7 月 KVM-4GB 60GB 2TB 洛杉矶全球[VPS测评]
- Sharktech→10G 无限流量服务器 55 折 三机房可选 支持支全球[VPS测评]
- 微基主机 7 月促销→美国主机 65 折优惠 日本 CN2 VPS 首月日本VPS[主机]
- RangCloud 推出山东联通NATVPS 256m内存1T流量100全球[VPS测评]
- 阿里云国际版新加坡机房轻量应用服务器VPS测评,延迟和丢包非常低,适合建全球[VPS测评]
- Foremo-便宜荷兰 1.5€ VPS 测评 G口不限流量全球[VPS测评]
- 潮科技|阿里云发布第七代高主频云服务器ECS和含光800云服务器全球[VPS测评]
- 牛信云入选艾瑞《2022年中国跨境电商服务行业趋势报告》典型厂商全球[VPS测评]
- 详细介绍vps云服务器及其作用全球[VPS测评]
- wikihost微基主机服务,香港稳定虚拟主机8折,3.73元/月起,C香港VPS[主机]
- 桔子数据,香港安畅BGP大带宽VPS 首月五折体验 三网直连 14元/月香港VPS[主机]
- 福州职业技术学院获得世界职业院校技能大赛云计算赛项银牌全球[VPS测评]
- 二三互联年末回馈:香港/美国云服务器新购特价促销,优惠码可打76折美国VPS[主机]
- tmhhost:8折优惠,美国cn2 gia高防vps,香港NTT vp美国VPS[主机]
- 六六云:1核1GB/15GB空间/1T流量/100Mbps/KVM/香港虚拟空间(主机)
- UCloud香港云服务器:免备案,CN2,全球31个机房,香港2核4G2香港VPS[主机]
- 2022年最新的15个美容院抖音文案,美容院文案怎么写全球[VPS测评]
- 创意加载进度HTML动画特效,SVG+CSS3实现的动画死神来了load全球[VPS测评]
- sugarhosts糖果主机黑五:VPS服务器5折,最低月付24.5元,香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-226266.html











