From 8c9dd30c5216af880ed8b53983d5070cd5908ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E5=AD=90?= Date: Sat, 31 Aug 2024 10:57:54 +0800 Subject: [PATCH] 0.6.0 --- functions.php | 2 +- postlist.php | 141 ++++++++++++++++++++++++-------------------------- 2 files changed, 70 insertions(+), 73 deletions(-) diff --git a/functions.php b/functions.php index a5a1bea..aebc015 100644 --- a/functions.php +++ b/functions.php @@ -5,7 +5,7 @@ function themeConfig($form) { $form->addInput($logoUrl); $icoUrl = new Typecho_Widget_Helper_Form_Element_Text('icoUrl', NULL, NULL, _t('站点 Favicon 地址')); $form->addInput($icoUrl); - $sticky = new Typecho_Widget_Helper_Form_Element_Text('sticky', NULL, '0', _t('置顶文章cid'), _t('多篇文章以`|`符号隔开'), _t('会在首页展示置顶文章。')); + $sticky = new Typecho_Widget_Helper_Form_Element_Text('sticky', NULL, NULL, _t('置顶文章cid'), _t('多篇文章以`|`符号隔开'), _t('会在首页展示置顶文章。')); $form->addInput($sticky); $showProfile = new Typecho_Widget_Helper_Form_Element_Radio('showProfile', array('0'=> _t('否'), '1'=> _t('是')), diff --git a/postlist.php b/postlist.php index 5b4f18d..0906e2f 100644 --- a/postlist.php +++ b/postlist.php @@ -4,101 +4,98 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; /** 文章置顶 */ $sticky = $this->options->sticky; // 置顶的文章id,多个用|隔开 +$db = Typecho_Db::get(); +$pageSize = $this->options->pageSize; -if ($sticky) { +if ($sticky && !empty(trim($sticky))) { $sticky_cids = array_filter(explode('|', $sticky)); // 分割文本并过滤空值 - $sticky_html = " 置顶 "; // 置顶标题的 html - $db = Typecho_Db::get(); - $pageSize = $this->options->pageSize; + if (!empty($sticky_cids)) { + $sticky_html = " 置顶 "; // 置顶标题的 html - // 清空原有文章的队列 - $this->row = []; - $this->stack = []; - $this->length = 0; + // 清空原有文章的队列 + $this->row = []; + $this->stack = []; + $this->length = 0; - // 获取总数,并排除置顶文章数量 - if (isset($this->currentPage) && $this->currentPage == 1) { - $totalOriginal = $this->getTotal(); - $stickyCount = count($sticky_cids); - $this->setTotal(max($totalOriginal - $stickyCount, 0)); + // 获取总数,并排除置顶文章数量 + if (isset($this->currentPage) && $this->currentPage == 1) { + $totalOriginal = $this->getTotal(); + $stickyCount = count($sticky_cids); + $this->setTotal(max($totalOriginal - $stickyCount, 0)); - // 构建置顶文章的查询 - $selectSticky = $this->select()->where('type = ?', 'post'); - foreach ($sticky_cids as $i => $cid) { - if ($i == 0) - $selectSticky->where('cid = ?', $cid); - else - $selectSticky->orWhere('cid = ?', $cid); + // 构建置顶文章的查询 + $selectSticky = $this->select()->where('type = ?', 'post'); + foreach ($sticky_cids as $i => $cid) { + if ($i == 0) + $selectSticky->where('cid = ?', $cid); + else + $selectSticky->orWhere('cid = ?', $cid); + } + + // 获取置顶文章 + $stickyPosts = $db->fetchAll($selectSticky); + + // 压入置顶文章到文章队列 + foreach ($stickyPosts as &$stickyPost) { + $stickyPost['title'] .= $sticky_html; + $this->push($stickyPost); + } + + $standardPageSize = $pageSize - count($stickyPosts); + } else { + $standardPageSize = $pageSize; } - // 获取置顶文章 - $stickyPosts = $db->fetchAll($selectSticky); - - // 压入置顶文章到文章队列 - foreach ($stickyPosts as &$stickyPost) { - $stickyPost['title'] .= $sticky_html; - $this->push($stickyPost); - } + // 构建正常文章查询,排除置顶文章 + $selectNormal = $this->select() + ->where('type = ?', 'post') + ->where('status = ?', 'publish') + ->where('created < ?', time()) + ->order('created', Typecho_Db::SORT_DESC) + ->page(isset($this->currentPage) ? $this->currentPage : 1, $standardPageSize); - $standardPageSize = $pageSize - count($stickyPosts); + foreach ($sticky_cids as $cid) { + $selectNormal->where('table.contents.cid != ?', $cid); + } } else { - $standardPageSize = $pageSize; - } - - // 构建正常文章查询,排除置顶文章 - $selectNormal = $this->select() - ->where('type = ?', 'post') - ->where('status = ?', 'publish') - ->where('created < ?', time()) - ->order('created', Typecho_Db::SORT_DESC) - ->page(isset($this->currentPage) ? $this->currentPage : 1, $standardPageSize); - - foreach ($sticky_cids as $cid) { - $selectNormal->where('table.contents.cid != ?', $cid); - } - - // 登录用户显示私密文章 - if ($this->user->hasLogin()) { - $uid = $this->user->uid; - if ($uid) { - $selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private'); - } - } - - $normalPosts = $db->fetchAll($selectNormal); - - // 压入正常文章到文章队列 - foreach ($normalPosts as $normalPost) { - $this->push($normalPost); + // 如果sticky_cids为空,使用默认查询 + $selectNormal = $this->select() + ->where('type = ?', 'post') + ->where('status = ?', 'publish') + ->where('created < ?', time()) + ->order('created', Typecho_Db::SORT_DESC) + ->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize); } } else { - // 如果没有置顶文章,正常分页 + // 如果没有置顶文章,使用默认查询 $selectNormal = $this->select() ->where('type = ?', 'post') ->where('status = ?', 'publish') ->where('created < ?', time()) ->order('created', Typecho_Db::SORT_DESC) - ->page(isset($this->currentPage) ? $this->currentPage : 1, $this->options->pageSize); + ->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize); +} - // 登录用户显示私密文章 - if ($this->user->hasLogin()) { - $uid = $this->user->uid; - if ($uid) { - $selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private'); - } +// 登录用户显示私密文章 +if ($this->user->hasLogin()) { + $uid = $this->user->uid; + if ($uid) { + $selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private'); } +} - $normalPosts = $db->fetchAll($selectNormal); - - // 清空原有文章的队列 +$normalPosts = $db->fetchAll($selectNormal); + +// 如果之前没有清空队列(没有置顶文章的情况),现在清空 +if (empty($sticky) || empty(trim($sticky)) || empty($sticky_cids)) { $this->row = []; $this->stack = []; $this->length = 0; +} - // 压入正常文章到文章队列 - foreach ($normalPosts as $normalPost) { - $this->push($normalPost); - } +// 压入正常文章到文章队列 +foreach ($normalPosts as $normalPost) { + $this->push($normalPost); } ?> next()): ?>