dedecms首頁(yè)調(diào)用隨機(jī)文章及自動(dòng)更新功能實(shí)例

字號(hào):


    本文實(shí)例講述了dedecms首頁(yè)調(diào)用隨機(jī)文章及自動(dòng)更新功能的實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
    我們知道織夢(mèng)多數(shù)情況下是生成靜態(tài)的html文件的,這樣一方面可以減少服務(wù)器的負(fù)荷,另一方面也是為了優(yōu)化,但是織夢(mèng)本身靜態(tài)要手動(dòng)更新生成,不是自動(dòng)的,今天我們就來(lái)說(shuō)一下怎樣實(shí)現(xiàn)自動(dòng)更新.
    (1)調(diào)用隨機(jī)文章:
    織夢(mèng)給出了隨機(jī)文章調(diào)用的參數(shù)如下:
    復(fù)制代碼代碼如下:{dede:arclist sort='rand' titlelen=48 row=16}
    <li><a href="[field:arcurl/]" target="_blank">[field:title/]</a></li>
    {/dede:arclist}
    (2)置定時(shí)自動(dòng)更新文件:
    復(fù)制下面代碼,粘貼到一個(gè)新文件中,命名為:autoindex.php,上傳到ftp的plus文件夾中,看清楚一點(diǎn)是plus文件夾中,錯(cuò)了位置不會(huì)生效.
    復(fù)制代碼代碼如下:<?php
    function sp_input( $text )
    {
    $text = trim( $text );
    $text = htmlspecialchars( $text );
    if (!get_magic_quotes_gpc())
    return addslashes( $text );
    else
    return $text;
    }
    $autotime = 3600;//自動(dòng)更新時(shí)間,單位為秒,這里我設(shè)為一小時(shí),大家可以自行更改。
    $fpath = "../data/last_time.inc";//記錄更新時(shí)間文件,如果不能達(dá)到目的,請(qǐng)檢查是否有讀取權(quán)限。
    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";//這里是首頁(yè)模板位置,當(dāng)前是dede默認(rèn)首面位置。
    $position = "../index.html";
    $homeFile = dirname(__FILE__)."/".$position;
    $homeFile = str_replace("\\", "/", $homeFile );
    $homeFile = str_replace( "http://", "/", $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 );
    }
    ?>
    然后我們需要在首頁(yè)的模版代碼head標(biāo)簽中加入一段代碼:
    復(fù)制代碼代碼如下:<script src="/plus/autoindex.php" language="javascript"></script>
    然后點(diǎn)擊后臺(tái)生成,更新首頁(yè),到此就ok了啊.
    希望本文所述對(duì)大家的dedecms建站有所幫助。