织梦CMS点击率统计实现方法

织梦CMS点击率统计实现方法

数据库字段添加

在对应内容模型的数据表中添加点击数字段(如click),确保字段类型为整型(INT)。

模板文件修改

在内容页模板(article_article.htm)中添加点击统计代码:

php 复制代码
{dede:field name='id' runphp='yes'}  
global $dsql;  
$dsql->ExecuteNoneQuery("UPDATE `dede_archives` SET `click` = `click` + 1 WHERE id = @me");  
@me = $dsql->GetOne("SELECT `click` FROM `dede_archives` WHERE id = @me")['click'];  
{/dede:field}  

AJAX异步统计(可选)

通过jQuery实现无刷新点击统计:

javascript 复制代码
$.post("/plus/count.php", {aid: {dede:field.id/}}, function(data) {  
    $("#click-count").html(data);  
});  

需在/plus/count.php中编写PHP处理逻辑更新数据库。


时间显示与格式化操作

模板时间调用

直接调用系统默认时间标签:

html 复制代码
发布时间:{dede:field.pubdate function="MyDate('Y-m-d H:i:s',@me)"/}  

自定义时间格式

修改/include/helpers/extend.helper.php,添加自定义时间函数:

php 复制代码
function custom_date($timestamp, $format = 'm/d/Y') {  
    return date($format, $timestamp);  
}  

模板调用方式:

html 复制代码
{dede:field.pubdate function="custom_date(@me, 'Y年m月d日')"/}  

时间差计算

显示"X分钟前"等相对时间:

php 复制代码
function time_ago($time) {  
    $diff = time() - $time;  
    if ($diff < 60) return $diff . "秒前";  
    elseif ($diff < 3600) return floor($diff/60) . "分钟前";  
    elseif ($diff < 86400) return floor($diff/3600) . "小时前";  
    else return floor($diff/86400) . "天前";  
}  

模板调用示例:

html 复制代码
{dede:field.pubdate function="time_ago(@me)"/}  

注意事项

  • 数据库操作需确保表前缀与系统一致(默认为dede_)。
  • AJAX WWW.Sf123.Sh.CN统计需检查服务器是否允许POST请求。
  • 时间函数修改后需清理模板缓存生效。
相关推荐
Xaire2 天前
行行查案例-数据解密-国密s4-webpack打包模块补齐
okhttp
霸道流氓气质2 天前
Spring AI Ollama 连接超时问题排查与解决:OkHttp 读超时配置全指南
人工智能·spring·okhttp
你觉得脆皮鸡好吃吗12 天前
XSS渗透 COOKIE
网络·http·okhttp·网络安全学习
彭于晏Yan15 天前
OkHttp 与 RestTemplate 技术选型对比
java·spring boot·后端·okhttp
JohnnyDeng9415 天前
OkHttp 拦截器链与缓存策略:深度解析网络层的核心机制
okhttp·缓存
胖胖胖胖胖虎16 天前
okhttp Stream Load 含认证请求重定向
starrocks·okhttp
步十人16 天前
【JavaScript】通过AJAX技术让前端发请求到后端
javascript·ajax·okhttp
XiYang-DING16 天前
【Spring】 Ajax
spring·ajax·okhttp
hj104317 天前
fastadmin 开发示例:使用 layer 弹窗实现数据快速录入
okhttp
YF021121 天前
Android App 高效升级指南:OkDownload 多线程断点续传与全版本安装适配
android·okhttp·app