Typecho - Function Hiển Thị Bài Viết Ngẫu Nhiên

Typecho - Function Hiển Thị Bài Viết Ngẫu Nhiên

Admin
2023-01-02 / 2 评论 / 20 阅读 / 正在检测是否收录...
Nhắc nhở nhẹ:
Bài viết này được cập nhật lần cuối ngày 02/01/2023, Đã 627 ngày không có cập nhật gì mới, nếu nội dung hoặc hình ảnh không hợp lệ, vui lòng để lại tin nhắn để tác giả sửa lại。Thân và thông!

Tạo danh sách bài viết ngẫu nhiên cho Typecho

Hướng dẫn

Vào file function

Thêm function sau:

class Widget_Post_tongleisuiji extends Widget_Abstract_Contents
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }
    public function execute()
    {
    $adapterName = $this->db->getAdapterName();//兼容非MySQL数据库
    if($adapterName == 'pgsql' || $adapterName == 'Pdo_Pgsql' || $adapterName == 'Pdo_SQLite' || $adapterName == 'SQLite'){
        $order_by = 'RANDOM()';
    }else{
        $order_by = 'RAND()';
    }   
$select  = $this->select()->from('table.contents')
->join('table.relationships', 'table.contents.cid = table.relationships.cid');
if($this->parameter->mid>0){
$select->where('table.relationships.mid = ?', $this->parameter->mid);
}
$select->where('table.contents.cid <> ?', $this->parameter->cid)
->where("table.contents.password IS NULL OR table.contents.password = ''")
->where('table.contents.type = ?', 'post')
->limit($this->parameter->pageSize)
->order($order_by);
$this->db->fetchAll($select, array($this, 'push'));
    }
}

Dán code sau vào nơi cần hiển thị

<?php 
$mid='';//此参数为空时为随机文章,为分类mid时则为当前分类下的随机文章
$cid=0;//此参数填写当前文章的cid即可在随机文章时不输出当前文章
$size=5;//随机输出文章的数量
$this->widget('Widget_Post_tongleisuiji@suiji', 'mid='.$mid.'&pageSize='.$size.'&cid='.$cid)->to($to);?>
<?php if($to->have()): ?>
<?php while($to->next()): ?>
<!--文章内容开始-->
<?php $to->title(); ?>
<?php $to->excerpt(150, '...'); ?>
<?php $to->permalink() ?>
<!--文章内容结束-->
<?php endwhile; ?>
<?php endif; ?>

Nguồn

Sưu tầm bên nước bạn 表情

1

Bình luận (2)

Hủy bỏ
  1. Ảnh đại diện
    Đức
    iPhone · Safari
    @

    画图

    ️📝Trả lời

    ⛔Xóa

  2. Ảnh đại diện
    Admin Tác giả
    Windows 10 · Google Chrome
    @

    ôpipo

    ️📝Trả lời

    ⛔Xóa