Typecho自定义上下篇链接

No Reply , Posted in 代码 on April 27, 2014

在 functions.php 中编写

新版本

新版本里,已经支持更灵活的上下文输出:

$this->thePrev($format = '%s', $default = NULL, $custom = array(
    'title' => '',
    'tagClass' => ''
));

$custom 数组部分即为自定义内容,目前支持给上下文链接添加自定义的 CSS 类名、及输出文字,文字部分支持 html 代码:

$this->thePrev('%s', NULL, array('title' => '上一篇', 'tagClass' => 'prev-content'));

上面的代码会输出:

上一篇
老版本
/**
* 显示下一篇
*
* @access public
* @param string $default 如果没有下一篇,显示的默认文字
* @return void
*/
function theNext($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);
$content = $db->fetchRow($sql);
 
if ($content) {
$content = $widget->filter($content);
$link = '下一篇';
echo $link;
} else {
echo $default;
}
}
 
/**
* 显示上一篇
*
* @access public
* @param string $default 如果没有下一篇,显示的默认文字
* @return void
*/
function thePrev($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created < ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1);
$content = $db->fetchRow($sql);
 
if ($content) {
$content = $widget->filter($content);
$link = '上一篇';
echo $link;
} else {
echo $default;
}
}

调用代码:

<?php thePrev($this); ?> 和 <?php theNext($this); ?>

标签: NONE