dedecms首页调用随机文章全自动时时更新
dedecms织梦系统是全站生成静态html的,这个对搜索引擎比较友好,但是有时我们要调用文章,让蜘蛛每次来访问都感觉像是有添加新内容一样,要如何做到呢?
可以添加以下dedecms随机文章调用的参数来实现,代码加到当前模板下index.htm相应位置
1 2 3 | {dede:arclist sort=’rand’ titlelen=48 row=16} <li><a href= "[field:arcurl/]" title= "[field:title/]" target= "_blank" >[field:title/]</a></li> {/dede:arclist} |
这样每次在后台点击更新主页后,显示的文章都是不同的
但是那样还不够完善,只能算半自动,如果哪天没空没去手动生成,随机文章还是不会变化。能不能实现全自动更新呢?看看下面的方法
在/plus文件夹下新建一个autoindex.php,加入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php function sp_input( $text ) { $text = trim( $text ); $text = htmlspecialchars( $text ); if (!get_magic_quotes_gpc()) return addslashes ( $text ); else return $text ; } $autotime = 3600; //自动更新时间,单位为秒,这里我设为一小时,大家可以自行更改。 $fpath = "../data/last_time.inc" ; //记录更新时间文件,如果不能达到目的,请检查是否有读取权限。 include ( $fpath ); if ( empty ( $last_time )) $last_time = 0; if ( sp_input( $_GET [ 'renew' ])== "now" ) $last_time = 0; if ((time()- $last_time )>= $autotime ) { define( 'DEDEADMIN' , ereg_replace ( "[/\\]{1,}" , '/' ,dirname( __FILE__ ) ) ); require_once (DEDEADMIN. "/../include/common.inc.php" ); require_once (DEDEINC. "/arc.partview.class.php" ); /* $row = $dsql->GetOne("Select * From dede_homepageset"); $dsql->Close(); $templet=$row['templet']; $position=$row['position']; */ $templet = “tnbjh/index.htm”; //这里是首页模板位置,当前是dede默认首面位置。 $position = "../index.html" ; $homeFile = dirname( __FILE__ ). "/" . $position ; $homeFile = str_replace ( "\\" , "/" , $homeFile ); $homeFile = str_replace ( "//" , "/" , $homeFile ); $pv = new PartView(); $pv ->SetTemplet( $cfg_basedir . $cfg_templets_dir . "/" . $templet ); $pv -> SaveToHtml( $homeFile ); $pv -> Close(); $file = fopen ( $fpath , "w" ); fwrite( $file , "<?php\n" ); fwrite( $file , "\$last_time=" .time(). ";\n" ); fwrite( $file , '?>' ); fclose( $file ); } ?> |
然后我们需要在首页的模版代码head标签中加入一段代码:
1 | <script src= "/plus/autoindex.php" language= "javascript" ></script> |
然后点击后台生成,更新首页,KO
打完收工,^.^

加微信交流
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架