Java
75%
前端
40%
Spring
60%
SpringMVC
70%
SpringBoot
80%
Mybatis
60%
数据结构与算法
75%
正在阅读Typecho主题非插件置顶功能

Typecho主题非插件置顶功能

时间:/评论:无评论/阅读:94次

添加

在 index.php 的 $this->title(); 前面加上 $this->sticky();例如下面这段代码

    <h2 class="title"><a href="<?php $this->permalink() ?>"><?php $this->sticky(); $this->title() ?></a></h2>
    //其实就是这个
    <?php $this->sticky(); $this->title() ?>

创建后台设置选项:

在主题的 functions.php 文件中添加代码,创建一个新的后台设置选项来存储置顶文章的 cid

    // 在主题的 functions.php 文件中
    $stickyCids = new Typecho_Widget_Helper_Form_Element_Text('stickyCids', NULL, NULL, _t('置顶文章CID'), _t('在这里填入置顶文章CID,支持多填,用英文逗号隔开'));
    $form->addInput($stickyCids);
    $stickyCids->setAttribute('class', 'j-setting-content j-setting-global');

前台index.php添加

    <?php
    $stickyCids = $this->options->stickyCids;
    if ($stickyCids) {
        // 用英文逗号分割
        $sticky_cids = explode(',', $stickyCids);
        // 去除每个元素的空格并过滤掉空元素
        $sticky_cids = array_filter(array_map('trim', $sticky_cids), 'trim');
        $sticky_html = "<span style='color:red'>[置顶] </span>";
        $db = Typecho_Db::get();
        $pageSize = $this->options->pageSize;
        $select1 = $this->select()->where('type =?', 'post');
        $select2 = $this->select()->where('type =? && status =? && created <?', 'post', 'publish', time());
        $this->row = [];
        $this->stack = [];
        $this->length = 0;
        $order = '';
        $stickyCidMap = [];
        $whereClauses = [];
        $whereParams = [];
        foreach ($sticky_cids as $i => $cid) {
            $whereClauses[] = 'cid =?';
            $whereParams[] = $cid;
            $order.= " when ". $cid. " then ". $i;
            $select2->where('table.contents.cid!=?', $cid);
            $stickyCidMap[$cid] = $i;
        }
        if (!empty($whereClauses)) {
            $select1->where(implode(' OR ', $whereClauses),...$whereParams);
        }
        if (empty(trim($order))) {
            $orderBy = 'table.contents.created';
            $sort = Typecho_Db::SORT_DESC;
        } else {
            $orderBy = null;
            $sort = "(case cid$order end)";
        }
        if ($orderBy!== null) {
            $select1->order($orderBy, $sort);
        }
        if ($this->_currentPage == 1) {
            $stickyPosts = $db->fetchAll($select1);
            // 对获取到的置顶文章按照设置的顺序排序
            usort($stickyPosts, function ($a, $b) use ($stickyCidMap) {
                return $stickyCidMap[$a['cid']] - $stickyCidMap[$b['cid']];
            });
            foreach ($stickyPosts as $sticky_post) {
                $sticky_post['sticky'] = $sticky_html;
                $this->push($sticky_post);
            }
        }
        $uid = $this->user->uid;
        if ($uid) {
            $select2->orWhere('authorId =? && status =?', $uid, 'private');
        }
        $sticky_posts = $db->fetchAll($select2->order('table.contents.created', Typecho_Db::SORT_DESC)->page($this->_currentPage, $this->parameter->pageSize));
        foreach ($sticky_posts as $sticky_post) {
            $this->push($sticky_post);
        }
        $this->setTotal($this->getTotal() - count($sticky_cids));
    }
    ?>

来源:https://deepsea.xin/archives/77.html

博主关闭了所有页面的评论

收藏域名

xgk.pw 小归客
1314.xyz 一生一世