教程
Typecho实现文章上一篇下一篇功能
1411

在别人博客看到主题底部都带上一篇下一篇的跳转功能,注意到这一细节后才想到自己的博客主题没有带,夺笋啊这 ,不就是俩按钮么,那咱就看tp官网文档手动加下代码,简单实现一下文章内上一篇下一篇跳转功能。

图文教程

在主题的文件 function.php 中加入以下代码:

/**
* 显示上一篇
*/
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;
}
}
/**
* 显示下一篇
*/
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;
}
} 

如果没有上一篇或者下一篇,就会默认 $default 显示为空,最后我们直接放在合适的位置,通过代码调用即可。
调用代码:

<!--上一篇-->
<?php thePrev($this); ?>
<!--下一篇-->
<?php theNext($this); ?>

就差一步了,弄两个酷炫的按钮显示在文章底部就行了。

当然如果你觉得在最后一篇文章和第一篇文章按钮空出了不太美观的,可以把 $default 弄成那个空按钮输出,这样就左右最右对称了。



最后是不是也嫌弃默认的按钮太丑?这时候就要把我珍藏酷炫css按钮分享给你们了,希望你们喜欢。下载按钮

结语

感谢访问强仔博客,希望本文对你有所帮助!


  • 上一篇
  • 下一篇
  • 添加评论
    评论(5)
    夏目贵志
    夏目贵志

    夏目贵志
    .
    .

    你的网址很牛哇

    强仔
    welcome to qiangzai blog