直接利用artlist中的orderyb=rand 即可实例。例:{dede:arclist row=10 orderby=rand}<li><a href="[field:arcurl/]" target="_blank">[field:title/]</a></li>{/dede:arclist}
利用自定做法。增加以下dedecms自定义标签 randarc:function lib_randarc(&$ctag , &$refObj){ global $dsql; FillAttsDefault($ctag->CAttribute->Items ,"limit|1"); extract($ctag->CAttribute->Items); $s = ''; $tcp = new DedeTagParse(); $tcp->SetNameSpace("field",'[',']'); $tcp->LoadSource($ctag->GetInnerText()); $sql = $query = "SELECT se.*,tp.typedir,tp.typename,tp.isdefault,tp.defaultname,tp.namerule,tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath FROM `dede_archives` se LEFT JOIN `dede_arctype` tp ON se.typeid=tp.id WHERE 1 order by rand() limit $limit"; $dsql->Execute("f",$sql); while($row1 = $dsql->GetArray("f")){ $row1['arcurl'] = GetFileUrl($row1['id'],$row1['typeid'],$row1['senddate'],$row1['title'], $row1['ismake'], $row1['arcrank'],$row1['namerule'],$row1['typedir'],$row1['money'], $row1['filename'],$row1['moresite'],$row1['siteurl'],$row1['sitepath']); ($tcp->CTags as $k => $v ){ $tcp->Assign($k , $row1[$v->GetName()]); } $s.=$tcp->GetResult(); } return $s;}
在dedecms文章模板中调用。{dede:randarc limit=1}<a href=[field:arcurl/]>[field:title/]</a>{/dede:randarc}首页随机调用文章并定时自动更新这样很简单的就完成了dedecms文章页面随机调用一篇文章的功能要实现DEDE首页自动更新的前提是网站首页必须有随机文章,对于随机文章不懂的朋友看这里实现DEDE首页内页随机文章列表代码,熟悉的直接跳过。{dede:arclist sort=’rand’ titlelen=48 row=16}<li><a href=”[field:arcurl/]“ title=”[field:title/]“ target=”_blank”>[field:title/]</a></li>{/dede:arclist}把下面的代码复制下来新建一个文本文档粘贴保存,然后把文件名改为rrsc_cn.php中的,上传到根目录。修改首页模板,在网站首页的<head></head>这部分加入<script src=”/rrsc_cn.php” language=”javascript“></script>这段代码,然后更新首页。这时首页每次更新的内容来源就有了,只是还不会自动生成更新的html文件,下面来说让dede自动生成首页的具体方法:<?phpfunction sp_input( $text ){$text = trim( $text );$text = htmlspecialchars( $text );if ( !get_magic_quotes_gpc() )return addslashes( $text );elsereturn $text;}$autotime = 300;//自动更新时间,单位为秒,这里我设为5分钟,大家可以自行更改。$fpath = ”../data/last_time.inc”;//记录更新时间文件,如果不能达到目的,请检查是否有读取权限。( $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 = ”default/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 = ( $fpath, ”w” );fwrite( $file, ”<?phpn” );fwrite( $file, ”$last_time=”.time().”;n” );fwrite( $file, ’?>’ );fclose( $file );}?>