在typecho博客的使用中文章置顶是一项不可缺少的功能,handsome主题也集成了这一功能,但是还有很多没有实现,当然也可以使用插件实现,但是很多博主并不喜欢安装许多繁杂的插件,所以纯代码的形式也是更好的选择
跟 Sticky插件 一样
在 index.php 的 $this->title(); 前面加上 $this->sticky();可出现这段 html.
例: <h2 class="title"><a href="<?php $this->permalink() ?>"><?php $this->sticky(); $this->title() ?></a></h2>
代码放在主题下 index.php
中
/** 文章置顶 */
$sticky = '1'; //置顶的文章id,多个用|隔开
if($sticky){
$sticky_cids = explode('|',$sticky); //分割文本
$sticky_html = "<span style='color:red'>[置顶] </span>"; //置顶标题的 html
$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 = '';
foreach($sticky_cids as $i => $cid) {
if($i == 0) $select1->where('cid = ?', $cid);
else $select1->orWhere('cid = ?', $cid);
$order .= " when $cid then $i";
$select2->where('table.contents.cid != ?', $cid); //避免重复
}
if ($order) $select1->order(null,"(case cid$order end)"); //置顶文章的顺序 按 $sticky 中 文章ID顺序
if ($this->_currentPage == 1) foreach($db->fetchAll($select1) 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)); //置顶文章不计算在所有文章内
}
来自:MoeShin
3 条评论
您好,博主
可以投稿服务器或者scdn文章内容吗?
这边可以赞助高防scdn【国内外均可】【一年起】
并且会给您一个心意红包
国内正规持证公司
如果不方便发您的联系方式
以下是我的联系方式!
我的QQ是:2814841448
我都是通过修改文章发布日期来置顶,哈哈
用这个方法简单