最近没事就喜欢折腾博客,慢慢进行完善功能,这次主要记录上一篇/下一篇的代码
/** * 显示下一篇 * * @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; } }
将以上代码写入functions.php
调用代码如下:
<?php thePrev($this); ?> 和 <?php theNext($this); ?>
评论已关闭