如何让Typecho搭建的网站首页文章随机显示?各位大佬支支招!

我想让我搭建的这个网站实现首页文章每次进来随机出现,不知道怎么搞,请问给位大佬怎么搞?

最近在大家了这个使用 Typecho + MWordStar 主题的博客,想实现一个功能:
首页文章每次进来都能随机排列,而不是按时间顺序固定显示。
我自己试了改模板,也研究了一点 JavaScript,但还不太熟悉,想听听大家的实现思路。
欢迎大佬们留言指点,代码示例、思路分享、甚至批评都可以,跪谢!

我的博客模板下载地址在这里:https://github.com/changbin1997/MWordStar/releases

如果有现成的插件、脚本或者简单修改方法,也麻烦告诉我,我会在博客里标注鸣谢~

已有 16 条评论

  1. 我的随机文章是爬取的sitemap.xml哈哈,不知道动态里面有没有什么接口😀

  2. 可以看看这个论坛
    https://forum.typecho.org/viewtopic.php?t=24768

  3. 方法一:修改模板文件。
    编辑主题的 index.php,找到文章循环部分,通常是 <?php while($this->next()): ?>,替换默认查询,在文件开头添加自定义查询:
    <?php
    // 自定义查询:随机排序文章
    $this->widget('Widget_Archive@random', 'pageSize=' . $this->options->postsListSize . '&type=index', 'order=rand')

    ->to($this); // 覆盖默认查询

    ?>
    <?php while($this->next()): ?>

    <!-- 正常输出文章内容 --> <h2><?php $this->title() ?></h2> <div><?php $this->content() ?></div>

    <?php endwhile; ?>

    方法二:使用 Widget_Archive 钩子。
    在主题的 functions.php 中添加:
    // 在 Typecho 中,通过插件或主题函数修改查询
    Typecho_Plugin::factory('Widget_Archive')->index = array('YourPlugin_Modify', 'randomOrder');

    class YourPlugin_Modify {

    public static function randomOrder($archive, $select) { if ($archive->is('index')) { // 仅首页生效 $select->order('RAND()'); // 直接注入 SQL 随机排序 } return $select; }

    }

    1. @临时访问

      方法一:刚才没显示出来。

      <?php
      // 自定义查询:随机排序文章
      $this-&gt;widget('Widget_Archive@random', 'pageSize=' . $this->options->postsListSize . '&type=index', 'order=rand')

      -&gt;to($this); // 覆盖默认查询

      ?>
      <?php while($this->next()): ?>

      &lt;!-- 正常输出文章内容 --&gt; &lt;h2&gt;&lt;?php $this-&gt;title() ?&gt;&lt;/h2&gt; &lt;div&gt;&lt;?php $this-&gt;content() ?&gt;&lt;/div&gt;

      <?php endwhile; ?>

  4. 这基本都是固定方法了。写个函数直接调用即可。样式自己随意。https://chrison.cn/work/87.html

  5. <?php // 将 Typecho 默认的遍历输出文章列表 while($this->next()) {} 替换为以下

    $lopwon = [];

    while($this->next()) {

    $lopwon[] = clone $this;

    }

    shuffle($lopwon);

    foreach ($lopwon as $item) {

    $title = $item->title(); // 其他类同

    }

    ?>

  6. 嗯,评论内容被过滤了,进入后台编辑评论可以看到。

  7. Typecho有办法实现多语言吗

    1. @大奏鼓

      可以,明天发你

    2. @大奏鼓

      https://blog.fsky7.com/technology/31 去看看这位博主的教程,系统本身就自带开启即可。

发表评论