typecho上一篇下一篇自定义代码

这里多加了一个缩略图调用 只是方便以后自己美化

缩略图方法
`

function showThumbnail($widget)
{
$mr = '默认图片地址';
$attach = $widget->attachments(1)->attachment;
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
if (preg_match_all($pattern, $widget->content, $thumbUrl)) {
     echo $thumbUrl[1][0];
} elseif ($attach->isImage) {
  echo $attach->url;
} else {
    echo $mr;
}
}

`
下面是 上下篇的调用方法
`

//下一篇
function theNext($widget)
{
$t = Typecho_Widget::widget('Widget_Archive@1');//@的作用我之前也有讲过,就是用来区分的,这里的$t就是定义的        
$this
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.created <= ?', time())
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);//sql查询下一篇文章
$db->fetchAll($sql, array($t, 'push'));//这个代码就是如何将查询结果封到$this里的
return $t;//返回变量
}
//上一篇
function thePrev($widget)
{
$t = Typecho_Widget::widget('Widget_Archive@2');//@的作用我之前也有讲过,就是用来区分的,@后面参数随便只要和上边的不一样就行
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created < ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.created <= ?', time())
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1);//sql查询上一篇文章
$db->fetchAll($sql, array($t, 'push'));
return $t;//返回变量
}

`
接下来是调用方法 也可以自己参考自己去分别调用

`

<?php
$prev=thePrev($this);//调用函数并将函数值给变量
$next=theNext($this);//调用函数并将函数值给变量
 ?>
<?php if($prev->created<$this->created): ?><!--判断上一篇文章是否存在-->
<div class="col-12<?php if($next->created>$this->created){echo ' col-md-6 pr-1';} ?>">
<div class="mb-3"> <a href="<?php $prev->permalink(); ?>" title="<?php $prev->title(); ?>"><div     
class="overlay"></div> <img src="<?php showThumbnail($prev); ?>" alt="<?php $prev->title(); ?>"     
class="slimg"><div class="title"><div class="entry-meta"><span><?php $prev->date(); ?></span><span>    
</span></div><h4><?php $prev->title(); ?></h4></div> </a></div>
</div><?php endif; ?>
<!--
//讲解一下,$prev=thePrev($this);调用后,$prev->permalink就是上一篇文章的链接,$prev->title就是标题, 
//showThumbnail($prev)就是缩略图,就跟正常调用文章的语法一致,只是$this换成了$prev。
-->
<?php if($next->created>$this->created): ?><!--判断下一篇文章是否存在-->
<div class="col-12<?php if($prev->created<$this->created){echo ' col-md-6 pl-1';} ?>">
<div class="mb-3"> <a href="<?php $next->permalink() ?>" title="<?php $next->title(); ?>"><div     
class="overlay"></div> <img src="<?php showThumbnail($next); ?>" alt="<?php $next->title(); ?>" 
class="slimg"><div class="title"><div class="entry-meta"><span><?php $next->date(); ?></span><span> 
</span></div><h4><?php $next->title(); ?></h4></div> </a></div>
</div><?php endif; ?>

`

另外附带一个文本内容 内容就是以上内容!!!!
typechowen.rar


扫描二维码,在手机上阅读!