Typecho主题插件站
每一个作品都值得被记录

Typecho 自定义上一篇/下一篇的显示样式

Typecho爱好者开发日志 • 2200次浏览 • 发布 2018-05-18 • 更新 2018-05-18
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码

有Typecho中,默认的上一篇和下一篇函数有个缺陷,就是在没有文章的时候会显示一个没有了。这是我无法接受的。

改进方法:

/**
* 显示下一篇
*
* @access public
* @param string $default 如果没有下一篇, 显示的默认文字
* @return void
*/
function theNext($widget, $word = '下一篇', $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 = $word.': <a href="' . $content['permalink'] . '" title="' . $content['title'] . '">' . $content['title']. '</a>';
        echo $link;
    } else {
        echo $default;
    }
}

/**
* 显示上一篇
*
* @access public
* @param string $default 如果没有上一篇, 显示的默认文字
* @return void
*/
function thePrev($widget, $word = '上一篇', $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 = $word.': <a href="' . $content['permalink'] . '" title="' . $content['title'] . '">' . $content['title']  . '</a>';
        echo $link;
    } else {
        echo $default;
    }
}

将上面的代码加入到function.php里面就好。

调用方法

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

本文检索关键词:开发日志
厂商投放
添加新评论 »