博客簿

TE获取上一篇/下一篇的链接

时间:4年前   阅读:2061

文章上一篇与下一篇调用代码:

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

或者

<?php $this->thePrev('上一篇 : %s', '没有上一篇'); ?>
<?php $this->theNext('下一篇 : %s', '没有下一篇'); ?>

这样的会输出同时文章的标题和链接,不是很利于加样式。
不过1.0版的typecho,已经支持更灵活的上下文输出:

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

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

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

上面的代码会输出:

<a class="prev-content" href="{permalink}" title="{title}"><span>上一篇</span></a>

老版本(亲测:其实1.0版这么用也是可以的)

/**
* 显示下一篇
*
* @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 = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">下一篇</a>';
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 = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">上一篇</a>';
echo $link;
} else {
echo $default;
}
}

调用代码:

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

文章参考与http://t.160.me/33.html

上一篇:typecho当前页码和总页码的完美实现方式

下一篇:Typecho非插件实现文章阅读次数统计

网友评论