fix 置顶文章翻页显示的问题
This commit is contained in:
浪子 2024-08-01 11:34:58 +08:00
parent 044fff9185
commit e8b1f941b4
1 changed files with 67 additions and 31 deletions

View File

@ -7,11 +7,21 @@ $sticky = $this->options->sticky ; //置顶的文章id多个用|隔开
if ($sticky) { if ($sticky) {
$sticky_cids = array_filter(explode('|', $sticky)); // 分割文本并过滤空值 $sticky_cids = array_filter(explode('|', $sticky)); // 分割文本并过滤空值
$sticky_html = " <span class=sticky--post> 置顶 </span> "; //置顶标题的 html $sticky_html = " <span class='sticky--post'> 置顶 </span> "; // 置顶标题的 html
$db = Typecho_Db::get(); $db = Typecho_Db::get();
$pageSize = $this->options->pageSize; $pageSize = $this->options->pageSize;
// 清空原有文章的队列
$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));
// 构建置顶文章的查询 // 构建置顶文章的查询
$selectSticky = $this->select()->where('type = ?', 'post'); $selectSticky = $this->select()->where('type = ?', 'post');
foreach ($sticky_cids as $i => $cid) { foreach ($sticky_cids as $i => $cid) {
@ -21,27 +31,27 @@ if ($sticky) {
$selectSticky->orWhere('cid = ?', $cid); $selectSticky->orWhere('cid = ?', $cid);
} }
// 清空原有文章的列队 // 获取置顶文章
$this->row = [];
$this->stack = [];
$this->length = 0;
// 只在首页第一页展示置顶文章
if (($this->_currentPage || $this->currentPage) == 1) {
$stickyPosts = $db->fetchAll($selectSticky); $stickyPosts = $db->fetchAll($selectSticky);
foreach ($stickyPosts as $stickyPost) {
$stickyPost['title'] = $stickyPost['title'] . $sticky_html; // 压入置顶文章到文章队列
$this->push($stickyPost); //压入列队 foreach ($stickyPosts as &$stickyPost) {
} $stickyPost['title'] .= $sticky_html;
$this->push($stickyPost);
} }
// 构建普通文章的查询,排除置顶文章的 CID $standardPageSize = $pageSize - count($stickyPosts);
} else {
$standardPageSize = $pageSize;
}
// 构建正常文章查询,排除置顶文章
$selectNormal = $this->select() $selectNormal = $this->select()
->where('type = ?', 'post') ->where('type = ?', 'post')
->where('status = ?', 'publish') ->where('status = ?', 'publish')
->where('created < ?', time()) ->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC) ->order('created', Typecho_Db::SORT_DESC)
->page($this->_currentPage, $pageSize); ->page(isset($this->currentPage) ? $this->currentPage : 1, $standardPageSize);
foreach ($sticky_cids as $cid) { foreach ($sticky_cids as $cid) {
$selectNormal->where('table.contents.cid != ?', $cid); $selectNormal->where('table.contents.cid != ?', $cid);
@ -56,13 +66,39 @@ if ($sticky) {
} }
$normalPosts = $db->fetchAll($selectNormal); $normalPosts = $db->fetchAll($selectNormal);
// 压入正常文章到文章队列
foreach ($normalPosts as $normalPost) { foreach ($normalPosts as $normalPost) {
$this->push($normalPost); //压入列队 $this->push($normalPost);
}
} 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);
// 登录用户显示私密文章
if ($this->user->hasLogin()) {
$uid = $this->user->uid;
if ($uid) {
$selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private');
}
} }
// 设置总数(减去置顶文章数量,以进行正确的分页) $normalPosts = $db->fetchAll($selectNormal);
$total = $this->getTotal() - count($sticky_cids);
$this->setTotal(max($total, 0)); // 确保总数不为负数 // 清空原有文章的队列
$this->row = [];
$this->stack = [];
$this->length = 0;
// 压入正常文章到文章队列
foreach ($normalPosts as $normalPost) {
$this->push($normalPost);
}
} }
?> ?>
<?php while($this->next()): ?> <?php while($this->next()): ?>