typecho 超完美上一篇下一篇文章加文章缩略图

chen'mo
2021-03-03 / 0 评论 / 2,321 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年03月03日,已超过1121天没有更新,若内容或图片失效,请留言反馈。

最近被这一功能吸引了,之前的文章是泽泽社长写的,这是木灵鱼儿写的,还有小灯泡写的。先收藏,以后在学原理。
[post]1127[/post]

fundtions.php里添加获取文章上一篇,下一篇cid

/**
* 显示上一篇
*
* 如果没有下一篇,返回null
*/
function thePrevCid($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) {
    return $content["cid"];
  } else {
    return $default;
  }
};

/**
* 获取下一篇文章mid
*
* 如果没有下一篇,返回null
*/
function theNextCid($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) {
    return $content["cid"];
  } else {
    return $default;
  }
};

//获取文章缩略图,没有则随机
function get_ArticleThumbnail($widget){
  // 当文章无图片时的随机缩略图
  $rand = mt_rand(1, 45); // 随机 1-9 张缩略图
  // 缩略图加速
  $rand_url;
  if(!empty(Helper::options()->articleImgSpeed)){
    $rand_url = Helper::options()->articleImgSpeed;
  }else {
    $rand_url = $widget->widget('Widget_Options')->themeUrl . '/images/articles/';
  }
  $random =  $rand_url . $rand . '.jpg'; // 随机缩略图路径

  $attach = $widget->attachments(1)->attachment;
  $pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';

  //如果有自定义缩略图
  if($widget->fields->thumb) {
    return $widget->fields->thumb;
  }else if (preg_match_all($pattern, $widget->content, $thumbUrl) && strlen($thumbUrl[1][0]) > 7) {
      return $thumbUrl[1][0];
  } else if ($attach->isImage) {
      return $attach->url;
  } else {
      return $random;
  }
};

插入上下文章位置,一般位于post.php里。

<?php //获取到id ?>
<?php $prevId = thePrevCid($this);$nextId=theNextCid($this);?>

<!-- 上一篇 -->
<?php if(!empty($prevId)) : ?>
    <?php $this->widget('Widget_Archive@recommend'.$prevId, 'pageSize=1&type=post', 'cid='.$prevId)->to($prev);?>
      <a href="<?php $prev->permalink();?>">
       <img src="<?php echo get_ArticleThumbnail($prev);?>" alt="<?php $prev->title();?>">
        <h2 class="card-title"><?php $prev->title();?></h2>
      </a>
<?php endif; ?>

<!-- 下一篇 -->
<?php if(!empty($nextId)) : ?>
  <?php $this->widget('Widget_Archive@recommend'.$nextId, 'pageSize=1&type=post', 'cid='.$nextId)->to($next);?>
    <a href="<?php $next->permalink();?>">
      <img src="<?php echo get_ArticleThumbnail($next);?>" alt="<?php $next->title();?>">
       <h2 class="card-title"><?php $next->title();?></h2>
    </a>
<?php endif; ?>

PHP
其中$prev和$next的使用和$this是一样的,他能做到的,$prev和$next也可以做到,所以拓展性大大延伸。

这里我只是提供了php,html代码自己去构建结构,前端代码不多说。

利用JS给博客左上显示FPS帧数
« 上一篇 03-02
给你的Typecho博客添加一个相册吧,支持任何主题
下一篇 » 03-03

评论 (0)

插入图片