Typecho Wiki
每一个作品都值得被记录

Typecho上下篇文章代码输出文章缩略图

Typecho维基君Typecho教程 • 914次浏览 • 发布 2023-01-22 • 更新 2023-01-22
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

首先是丢进主题里面的function里面

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;//返回变量
}

调用:
一般在post.php或者page.php中使用

<?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; ?>

作者:Chen'mo
注意,需要自己调整css布局,,貌似会奇奇怪怪,输出是输出了

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:图片,主题
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »