背景

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

3周前 typecho帮助 11°C

在 functions.php 中编写
新版本里,已经支持更灵活的上下文输出:

$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>

老版本

/**
* 显示下一篇
*
* @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); ?>
版权声明:小站所有内容均为互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系删除。
本文链接:https://magicblue.cn/lost/15347.html
最后编辑: 23年09月15日

选择表情