详情 评论 问答 子比主题 – 数据统计条代码小柚子12月1日更新关注私信0387 这是一款子比主题的数据统计条代码,里面带有日更新、文章数目、帖子数量、评论数量、注册用户、运行时间、浏览次数等功能,喜欢的自行部署吧! 代码部署 定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,然后将下面的代码放里面即可! // 统计条func代码 if (!function_exists('get_posts_count_today')) { function get_posts_count_today($post_type = 'post') { $today = date('Y-m-d'); $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'date_query' => array( array( 'after' => $today . ' 00:00:00', 'before' => $today . ' 23:59:59', 'inclusive' => true, ), ), 'fields' => 'ids', 'nopaging' => true, ); $query = new WP_Query($args); return $query->post_count; } } add_action('wp_footer', 'output_site_stats_js_vars'); function output_site_stats_js_vars() { $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; $today_posts = function_exists('get_posts_count_today') ? get_posts_count_today() : 0; $uptime_days = floor((time() - strtotime("2023-11-11 11:11:11")) / 86400); global $wpdb; $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); $comments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'"); $post_view = function_exists('nd_get_all_view') ? nd_get_all_view() : get_option('zibi_pageviews_total', 0); echo '<script type="text/javascript"> var tj_wzzs = "' . esc_js($published_posts) . '"; var tj_jrgx = "' . esc_js($today_posts) . '"; var tj_yxsj = "' . esc_js($uptime_days) . '"; var tj_yhzs = "' . esc_js($users) . '"; var tj_plzs = "' . esc_js($comments) . '"; var tj_zll = "' . esc_js($post_view) . '"; </script>'; } 然后我们定位:WP后台–>>外观–>>小工具–>>自定义HTML,将下面的代码放里面,你想显示在哪里你就将这个小工具放到哪个位置即可! <div class="tuc-2ea177b2-475194-0 zibi-stats-bar flex items-center text-sm transition-colors duration-300 tuc-2ea177b2-475194-0" id="stats-bar"> <!-- 今日更新 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-refresh text-red-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">今日更新:</span><span class="tuc-2ea177b2-475194-0 font-bold text-red-600 tuc-2ea177b2-475194-0" id="today-posts-count">篇</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 文章数目 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-book text-blue-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">文章数目:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="posts-count">篇</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 帖子数量 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-sticky-note text-teal-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">帖子数量:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="zibi-posts-count">篇</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 评论数量 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-comments text-green-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">评论数量:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="comments-count">条</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 注册用户 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-users text-orange-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">注册用户:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="users-count">位</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 运行时间 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-clock-o text-purple-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">运行时间:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="uptime-count">天</span> </div> <div class="tuc-2ea177b2-475194-0 mx-2 text-gray-400 tuc-2ea177b2-475194-0">|</div> <!-- 浏览次数 --> <div class="tuc-2ea177b2-475194-0 stats-item flex items-center tuc-2ea177b2-475194-0"> <i class="tuc-2ea177b2-475194-0 fa fa-eye text-yellow-500 mr-1 tuc-2ea177b2-475194-0"></i> <span class="tuc-2ea177b2-475194-0 stats-label tuc-2ea177b2-475194-0">浏览次数:</span><span class="tuc-2ea177b2-475194-0 font-bold tuc-2ea177b2-475194-0" id="pageviews-count">w+</span> </div> </div> <style> /* 核心布局 */ /* 核心布局 */ .zibi-stats-bar { display: flex; align-items: center; border-radius: 6px; padding: 8px 12px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; width: 100%; box-sizing: border-box; } /* 图标与文字垂直居中对齐 */ .stats-item i { vertical-align: middle; /* 解决图标偏上问题 */ transform: translateY(-1px); /* 微调图标位置 */ } .zibi-stats-bar { display: flex; align-items: center; border-radius: 6px; padding: 8px 12px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; width: 100%; box-sizing: border-box; } /* 白天模式样式 */ :root:not(.dark) .zibi-stats-bar { background: #fff; border: 1px solid #ebeef5; color: #303133; } /* 夜间模式样式 */ :root.dark .zibi-stats-bar { background: #1e1e1e; border: 1px solid #2c2c2c; color: #c9c9c9; } /* 图标固定彩色样式 - 不随主题变化 */ #today-icon { color: #ff4d4f !important; } .stats-item:nth-child(3) i { color: #1890ff !important; } /* 文章图标 - 蓝色 */ .stats-item:nth-child(5) i { color: #13c2c2 !important; } /* 帖子图标 - 青色 */ .stats-item:nth-child(7) i { color: #52c41a !important; } /* 评论图标 - 绿色 */ .stats-item:nth-child(9) i { color: #fa8c16 !important; } /* 用户图标 - 橙色 */ .stats-item:nth-child(11) i { color: #722ed1 !important; } /* 时间图标 - 紫色 */ .stats-item:nth-child(13) i { color: #faad14 !important; } /* 浏览图标 - 黄色 */ /* 图标美化效果 */ .stats-item i { transition: all 0.3s ease; transform-origin: center; text-shadow: 0 1px 2px rgba(0,0,0,0.1); } .stats-item:hover i { transform: scale(1.15); filter: brightness(1.2); } /* 今日更新的特殊颜色 */ :root.dark #today-posts-count { color: #ff7a45; } :root:not(.dark) #today-posts-count { color: #ff4d4f; } /* 其他统计数字的暗色模式适配 */ :root.dark #posts-count { color: #91bfff; } :root.dark #zibi-posts-count { color: #89e8e8; } :root.dark #comments-count { color: #b7eb8f; } :root.dark #users-count { color: #ffd591; } :root.dark #uptime-count { color: #d3adf7; } :root.dark #pageviews-count { color: #ffe58f; } /* 白天模式数字颜色 */ :root:not(.dark) #posts-count { color: #1890ff; } :root:not(.dark) #zibi-posts-count { color: #13c2c2; } :root:not(.dark) #comments-count { color: #52c41a; } :root:not(.dark) #users-count { color: #fa8c16; } :root:not(.dark) #uptime-count { color: #722ed1; } :root:not(.dark) #pageviews-count { color: #faad14; } /* 分割线颜色 */ :root:not(.dark) .zibi-stats-bar > .text-gray-400 { color: #dcdde0; } :root.dark .zibi-stats-bar > .text-gray-400 { color: #4e4e4e; } /* 响应式适配:小屏幕自动换行 */ @media (max-width: 768px) { .zibi-stats-bar { flex-wrap: wrap; gap: 8px 12px; padding: 10px; justify-content: center; } .stats-item { font-size: 12px; margin: 2px 0; } .zibi-stats-bar > .text-gray-400 { display: none; /* 移除小屏幕分割线 */ } } /* 超小屏幕优化 */ @media (max-width: 375px) { .stats-item { font-size: 11px; flex: 1 0 40%; /* 每行显示2个统计项 */ justify-content: center; } } </style> <script> document.addEventListener('DOMContentLoaded', async () => { // 初始化夜间模式状态 updateDarkMode(); // 监听主题切换事件 window.addEventListener('theme-mode-changed', updateDarkMode); // 监听系统暗色模式变化 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateDarkMode); // 所有设备都执行统计数据获取 try { const [todayPostsCount, postCount, zibiPostsCount, commentCount, userCount, uptimeDays, pageviews] = await Promise.all([ getTodayPostsCount(), getPostCount(), getZibiPostsCount(), getCommentCount(), getUserCount(), getUptimeDays(), getPageviews(true) ]); // 更新DOM document.getElementById('today-posts-count').textContent = `${todayPostsCount}篇`; document.getElementById('posts-count').textContent = `${postCount}篇`; document.getElementById('zibi-posts-count').textContent = `${zibiPostsCount}篇`; document.getElementById('comments-count').textContent = `${commentCount}条`; document.getElementById('users-count').textContent = `${userCount}位`; document.getElementById('uptime-count').textContent = `${uptimeDays}天`; document.getElementById('pageviews-count').textContent = formatNumber(pageviews); } catch (error) { console.error('统计数据获取失败:', error); document.getElementById('today-posts-count').textContent = '获取失败'; } }); // 获取今日文章数 async function getTodayPostsCount() { try { const res = await fetch('/wp-json/zibi/v1/today_posts'); if (!res.ok) throw new Error('API请求失败'); const data = await res.json(); return data.postCount || 0; } catch (error) { console.error('[API] 获取今日文章数失败:', error); return 0; } } // 获取文章总数 async function getPostCount() { try { const res = await fetch('/wp-json/wp/v2/posts?per_page=1&_fields=id'); if (!res.ok) throw new Error('文章总数API请求失败'); return parseInt(res.headers.get('X-WP-Total') || 0); } catch (error) { console.error('[API] 获取文章总数失败:', error); return 0; } } // 获取帖子总数(适配forum_post类型) async function getZibiPostsCount() { try { const res = await fetch('/wp-json/zibi/v1/topic_count'); if (!res.ok) throw new Error('帖子API请求失败'); const data = await res.json(); return data.topicCount || 0; } catch (error) { console.error('[API] 获取子比帖子总数失败:', error); return 0; } } // 获取评论总数 async function getCommentCount() { try { const res = await fetch('/wp-json/wp/v2/comments?per_page=1&_fields=id'); if (!res.ok) throw new Error('评论总数API请求失败'); return parseInt(res.headers.get('X-WP-Total') || 0); } catch (error) { console.error('[API] 获取评论总数失败:', error); return 0; } } // 获取用户总数 async function getUserCount() { try { const res = await fetch('/wp-json/zibi/v1/stats'); if (!res.ok) throw new Error('用户总数API请求失败'); const data = await res.json(); return data.userCount || 0; } catch (error) { console.error('[API] 获取用户总数失败:', error); return 0; } } // 获取网站运行天数 function getUptimeDays() { const startDate = new Date('2023-11-11'); // 修改为你的上线日期 const currentDate = new Date(); const timeDiff = currentDate.getTime() - startDate.getTime(); return Math.floor(timeDiff / (1000 * 60 * 60 * 24)); } // 获取浏览次数并增加计数 async function getPageviews(increment = false) { try { const res = await fetch(`/wp-json/zibi/v1/pageviews?increment=${increment}`); if (!res.ok) throw new Error('浏览次数API请求失败'); const data = await res.json(); return data.pageviews || 0; } catch (error) { console.error('[API] 获取浏览次数失败:', error); return 0; } } // 数字格式化(转换为x.xw+格式) function formatNumber(num) { if (num >= 10000) { return (num / 10000).toFixed(1) + 'w+'; } return num.toString(); } // 更新夜间模式样式 function updateDarkMode() { const isDark = document.documentElement.classList.contains('dark') || window.matchMedia('(prefers-color-scheme: dark)').matches; if (isDark) { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } } </script> 文章很赞,支持一下吧~ 还没有人为TA充电 为TA充电 还没有人为TA充电 © 版权声明 版权声明 1 本站名称: 七九网单 2 本站网址:www.fengxue.cc 3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。 4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。 5 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。 6 本站附件资源、教程等内容如因时效原因失效或不可用,请评论区留言或联系站长及时更新。 THE END子比美化 喜欢就支持一下吧点赞7打赏 分享QQ空间微博QQ好友复制链接收藏如何下载资源?您可以通过搜索或浏览分类列表来找到您期望下载的资源。随后点击资源介绍页右侧的下载链接按钮,依据提示信息进行操作即可。是否需要充值才能下载?大部分资源可积分免费下载,为了维持网站的运行小部分资源须付费才能下载。下载的资源是否有版权?本站提供的下载资源均为网络搜集,仅供个人学习和交流使用。对于版权问题,请用户自行判断并承担相应责任。 上一篇 子比主题 – logo透明过渡美化样式 下一篇 子比主题 – 精美弹窗样式美化 评论 抢沙发 请登录后发表评论 登录 注册 暂无评论内容