Typecho自定义上下篇链接

时间:2020-08-01   阅读:410

Typecho自定义上下篇链接


在 functions.php 中编写
<div style="font-size: 30px;">新版本</div>

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

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


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

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


上面的代码会输出:

<span>上一篇</span>


<div style="font-size: 30px;">老版本</div>

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


调用代码:

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



上一篇:Typecho 创建自定义首页或独立页面

下一篇:Typecho评论模块个性化

网友评论