第1步:在首页模板中添加随机文章调用标签

  1. 编辑首页模板文件
    • 打开首页模板文件,通常位于 /templates/ 目录下。
  2. 添加随机文章调用标签
    • 在适当位置添加以下代码:
       
       
      {dede:arclist sort='rand' titlelen=48 row=16}
      <a href="[field:arcurl/]">[field:title/]</a><br />
      {/dede:arclist}
    • 这段代码会调用16篇随机文章,并在每次刷新动态页面时显示不同的文章。

第2步:设置定时自动更新文件

  1. 创建 autoindex.php 文件

    • 创建一个新的PHP文件,命名为 autoindex.php,并添加以下代码:
      <?php
      $fpath = DEDEDATA."/cache/autotime.txt";
      $fp = fopen($fpath, "r");
      $autotime = intval(fread($fp, filesize($fpath)));
      fclose($fp);
      
      if( time() >= $autotime )
      {
          define('DEDEADMIN', ereg_replace("/{1,}", "/", dirname(__FILE__)) );
          require_once(DEDEADMIN."/../include/common.inc.php");
          require_once(DEDEINC."/arc.partview.class.php");
      
          $templet = "index.htm"; // 首页模板位置
          $position = "index.html"; // 首页生成位置
      
          $homeFile = dirname(__FILE__).'/'.$position;
          $homeFile = str_replace("\\", "/", $homeFile);
          $homeFile = str_replace("https://", "/", $homeFile);
      
          $pv = new PartView();
          $pv->SetTemplet( $cfg_basedir.$cfg_templets_dir."/".$templet );
          $pv->SaveToHtml( $homeFile );
          $pv->Close();
      
          $file = fopen( $fpath, "w" );
          fwrite( $file, time() + 3600 ); // 设置下次更新时间为1小时后
          fclose( $file );
      }
      ?>
    • 保存文件并上传到网站根目录的 plus 文件夹中。
  2. 在首页模板中引入 autoindex.php 文件

    • 打开首页模板文件,在 <head> 标签中添加以下代码:
       
       
      <script type="text/javascript" src="/plus/autoindex.php"></script>