本篇文章转载于:泽泽社长Typecho上下篇文章代码输出文章缩略图
虽说是教大家如何调用上下篇文章缩略图的,但是实则是探讨一种船新的函数写法。在正式教程开始之前,首先我们需要先写个缩列图函数,如下:
主题function.php添加函数:
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; } }
函数调用方法也很简单,一般如<?php showThumbnail($this); ?>,参数是$this,问题的关键点就是如何找到上下篇文章的$this,几年前的给别人弄的时候选择了一种比较繁琐的方式,就是先查到上下篇文章的cid,然后根据《Typecho根据文章cid获取文章信息》提到的方式,调用出对应的$this,这种方式缺点就是代码多了一些数据查询也多了两次。
2021年开年之际,我又投入时间研究了这个问题,经过大量失败后所幸还是研究出成果了。原理就是自己写两个函数用来输出上下篇文章,关键点也是在$this参数上,折腾了好久,具体代码如下。
函数部分
//下一篇 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; ?>