优选主流主机商
任何主机均需规范使用

如何实现DedeCMS首页随机文章调用及自动更新功能详解

本文实例讲述了dedecms首页调用随机文章及自动更新功能的实现方法。分享给大家供大家参考。具体实现方法如下:

我们知道织梦多数情况下是生成静态的html文件的,这样一方面可以减少服务器的负荷,另一方面也是为了优化,但是织梦本身静态要手动更新生成,不是自动的,今天我们就来说一下怎样实现自动更新.

(1)调用随机文章:

织梦给出了随机文章调用的参数如下:

  1. {dede:arclist sort=’rand’ titlelen=48 row=16}
  2. <li><a href=”[field:arcurl/]” title=”[field:title/]” target=”_blank”>[field:title/]</a></li>
  3. {/dede:arclist}

(2)置定时自动更新文件:

复制下面代码,粘贴到一个新文件中,命名为:autoindex.php,上传到ftp的plus文件夹中,看清楚一点是plus文件夹中,错了位置不会生效.

代码如下:

<?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( emptyempty($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”);

$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 “);

fwrite( $file,”$last_time=”.time().”; “);

fwrite( $file, ‘?>’ );

fclose( $file );

}

?>

然后我们需要在首页的模版代码head标签中加入一段代码:

代码如下:

然后点击后台生成,更新首页,到此就ok了啊。

未经允许不得转载:搬瓦工中文网 » 如何实现DedeCMS首页随机文章调用及自动更新功能详解