织梦dedecms首页调用随机文章并定时自动更新
类别:建站知识   时间:03-29

织梦首页静态化以后,如果不重新生成,它是不变的,即便你在首页随机调用文档也不会自动更新。但是静态页面又不能使用php功能,那又要用到js来引入php文件,才能行得通。下面小编直接告诉大家方法:

在plus目录下新建一个名为autoupdate.php的php文件写入一下代码:

<?$updatetime&nbsp;=&nbsp;1800;//自动更新时间,单位为秒,这里我设为一小时,大家可以自行更改。$fpath&nbsp;=&nbsp;"../data/update_time.inc";//记录更新时间文件,如果不能达到目的,请检查是否有读取权限。include(&nbsp;$fpath&nbsp;);if(empty($last_time)){$last_time&nbsp;=&nbsp;0;}if((time()-$last_time)>=$updatetime&nbsp;){define('DEDEADMIN',&nbsp;ereg_replace("[/\\]{1,}",'/',dirname(__FILE__)&nbsp;)&nbsp;);require_once(DEDEADMIN."/../include/common.inc.php");require_once(DEDEINC."/arc.partview.class.php");$templet&nbsp;=&nbsp;"xunmzy/index.htm";//这里是首页模板位置,当前是dede默认首面位置。$position&nbsp;=&nbsp;"../index.html";$homeFile&nbsp;=&nbsp;dirname(__FILE__)."/".$position;$homeFile&nbsp;=&nbsp;str_replace("\\",&nbsp;"/",&nbsp;$homeFile&nbsp;);$homeFile&nbsp;=&nbsp;str_replace(&nbsp;"//",&nbsp;"/",&nbsp;$homeFile&nbsp;);$pv&nbsp;=&nbsp;new&nbsp;PartView();$pv&nbsp;->&nbsp;SetTemplet(&nbsp;$cfg_basedir.$cfg_templets_dir."/".$templet&nbsp;);$pv&nbsp;->&nbsp;SaveToHtml(&nbsp;$homeFile&nbsp;);$pv&nbsp;->&nbsp;Close();$file&nbsp;=&nbsp;fopen(&nbsp;$fpath,&nbsp;"w");fwrite(&nbsp;$file,&nbsp;"<?php\n");fwrite(&nbsp;$file,"\$last_time=".time().";\n");fwrite(&nbsp;$file,&nbsp;'?>'&nbsp;);fclose(&nbsp;$file&nbsp;);}?>

然后在首页插入一下js:

<script src="/plus/autoupdate.php" type="text/javascript"></script>

最后重新生成首页,做完这些就完成了调用随机文章并定时自动更新

智慧半岛,是一家专注于青岛网站建设青岛网站开发的青岛网络公司,Copyright ©2007-2019 Ebandao.Cn 版权所有 All Rights Reserved.(建议在1366x768以上的浏览器中访问)鲁ICP备08014496号

13153297890