本文介绍了织梦dedecms怎么定时自动更新随机文章,解决思路:先在首页模板里面调用随机代码,然后新建一个PHP文件,将教程里面的代码放进去,最后在HEAD标签中引入这个PHP文件就可以了。
织梦dedecms怎么定时自动更新随机文章?网站首页的内容需要随时更新,但有时我们不能随时手动更新,这时可以为dedecms设置首页自动更新,下面看看织梦定时更新文章的方法。
第1步、在首页模板里面添加随机文章调用标签,具体调用代码如下:
{dede:arclist sort=’rand’ titlelen=48 row=16}
<li><a href="[field:arcurl/]" title="[field:title/]" target="_blank">[field:title/]</a></li>
{/dede:arclist}
上面这段列表代码可以调用出随机文章,并且在每次刷新动态页面的时候都会变化,但是由于织梦是首页生成静态html的,所以如果不去手动生成还是不会变化,这样就用到了下面的方法。
第2步、设置定时自动更新文件
复制下面代码,粘贴到一个新文件中,命名为:autoindex.php,上传到网站根目录的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( 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默认首面位置。www.dede58.com<a href="http://www.dede58.com/" target="_blank"><u>织梦模板</u></a>
$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 );
}
?>
3、我们需要在首页的模版代码head标签中引入下面这段代码,也就是引入这个php文件:
<script src="/plus/autoindex.php" language="javascript"></script>
完成以上步骤后在后台点击生成-更新首页。默认3600秒是一小时,大家可以自行更改。
上面就是织梦定时更新文章的介绍,若有疑问可以找小编进一步探讨交流~