前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Typecho如何利用自定义页面,随机显示(跳转到)一篇文章

Typecho如何利用自定义页面,随机显示(跳转到)一篇文章

作者头像
杨逸轩
发布2018-06-14 10:07:22
8430
发布2018-06-14 10:07:22
举报
文章被收录于专栏:杨逸轩 ' sBlog杨逸轩 ' sBlog

当我们的博客文章很多时,无法很直观的展示所有文章给读者,这时候我们可随机给读者显示一篇文章。 下面这个方法也是在网上找的一些教程累积的方法,不需要修改typecho源文件,只需要在主题文件夹下,新建一个php文件,名字自定义,然后把以下代码扔进去:

代码语言:javascript
复制
<?php
    /**
     * 手气最佳
     *
     * @package custom
     */
    if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
    <?php
    $db = Typecho_Db::get();
    $sql = $db->select('MAX(cid)')->from('table.contents')
        ->where('status = ?','publish')
        ->where('type = ?', 'post')
        ->where('created <= unix_timestamp(now())', 'post');
    $result = $db->fetchAll($sql);
    $max_id = $result[0]['MAX(`cid`)'];//POST类型数据最大的CID
    $sql = $db->select('MIN(cid)')->from('table.contents')
        ->where('status = ?','publish')
        ->where('type = ?', 'post')
        ->where('created <= unix_timestamp(now())', 'post');
    $result = $db->fetchAll($sql);
    $min_id = $result[0]['MIN(`cid`)'];//POST类型数据最小的CID
    $result = NULL;
    while($result == NULL) {
        $rand_id = mt_rand($min_id,$max_id);
        $sql = $db->select()->from('table.contents')
            ->where('status = ?','publish')
            ->where('type = ?', 'post')
            ->where('created <= unix_timestamp(now())', 'post')
            ->where('cid = ?',$rand_id);
        $result = $db->fetchAll($sql);
    }
    ?>
    <!--下面就是实现显示跳转到随机文章-->
    <?php $target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result['0']); ?>
    <!--跳转-->
      <?php $this->response->redirect($target['permalink'],307); ?>

好了,然后在后要新建一个自定义页面,引用这个php文件即可。 实际效果请看本博客导航栏里的【手气】 感谢~

转载至 :吖远zzy

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档