记录 Pinghsu 主题的一些二次调整

22

我的博客使用 [chakhsu](https://www.linpx.com) 开源的 [Pinghsu](https://github.com/chakhsu/pinghsu) 主题,使用这款主题的主要原因在于其更注重**内容写作本身**,没有那么多花里胡哨的东西,非常简约,在此也非常感谢这位作者,你的良心大大滴好😋。

主题开源地址:https://github.com/chakhsu/pinghsu

下面记录一些我对该主题做出的一些调整,我不会 PHP 语言,都是按照语言特性,照葫芦画瓢修改的🤣,如果有更好的方案,欢迎留言给我。

将最新的评论展示在前面

主题默认是按时间先后,最新的评论排在了最后面,我希望最新的评论排在前面,下面是我的调整:

找到主题目录下的 functions.php 文件中的 themeInit 函数:

function themeInit($archive){
    Helper::options()->commentsMaxNestingLevels = 999;
    if ($archive->is('archive')) {
        $archive->parameter->pageSize = 12;
    }
}

追加一段代码:

function themeInit($archive){
    Helper::options()->commentsMaxNestingLevels = 999;
    if ($archive->is('archive')) {
        $archive->parameter->pageSize = 12;
    }
     //将最新的评论展示在前
    Helper::options()->commentsOrder = 'DESC'; // <--- 这一行
}

评论头像显示优化

在主题目录下的 functions.php 文件中,增加下面这个函数:

/**
 * 获取评论头像
 * @param $mail 邮箱
 * @package $isOwner 是否为作者
 */
function getAvatarByMail($mail, $isOwner = false){
    if ($isOwner) {
        return '你自己的头像url地址';
    }
    $gravatarsUrl = 'https://cravatar.cn/avatar/';
    $mailLower = strtolower($mail);
    $md5MailLower = md5($mailLower);
    $qqMail = str_replace('@qq.com', '', $mailLower);
    if (strstr($mailLower, "qq.com") && is_numeric($qqMail) && strlen($qqMail) < 11 && strlen($qqMail) > 4) {
        return 'https://thirdqq.qlogo.cn/g?b=qq&nk=' . $qqMail . '&s=100';
    } else {
        return $gravatarsUrl . $md5MailLower . '?d=mm';
    }
}

然后调整主题目录下的 comments.php 文件,默认在第50行处,原代码:

<img class="avatar" src="<?php echo $avatar ?>" width="<?php echo $size ?>" height="<?php echo $size ?>" />

$avatar 修改为 getAvatarByMail($comments->mail, $comments->authorId == $comments->ownerId);,如下:

<img class="avatar" src="<?php echo getAvatarByMail($comments->mail, $comments->authorId == $comments->ownerId); ?>" width="<?php echo $size ?>" height="<?php echo $size ?>" />

首页文章封面图只使用自定义的图片(thumb)

主题默认的文章封面图调用顺序:文章自定义字段 thumb -> 附件第一张图片 -> 文章图片 -> 默认缩略图 -> 随机图片 -> 无

我只希望设置了文章的thumb自定义字段,才显示封面图,其他情况不显示,下面是我的修改:

调整主题目录下的 index.php 文件,默认在74行-85行的这段 a 标签:

<a href="<?php $this->permalink(); ?>">
<?php if (array_key_exists('thumb',unserialize($this->___fields()))): ?>
	<div class="onelist-item-thumb <?php if ($this->options->colorBgPosts == 'defaultColor'): ?> bg-deepgrey<?php elseif ($this->options->colorBgPosts == 'customColor'): ?><?php if (array_key_exists('green',unserialize($this->___fields()))): ?> bg-green<?php elseif (array_key_exists('red',unserialize($this->___fields()))): ?> bg-red<?php elseif (array_key_exists('yellow',unserialize($this->___fields()))): ?> bg-yellow<?php elseif (array_key_exists('blue',unserialize($this->___fields()))): ?> bg-blue<?php elseif (array_key_exists('purple',unserialize($this->___fields()))): ?> bg-purple<?php else : ?> bg-<?php echo randBgColor(); ?><?php endif; ?><?php endif; ?>" style="background-image:url(<?php parseFieldsThumb($this);?>);"></div>
<?php else : ?>
<?php $thumb = showThumb($this,null,true);?>
<?php if(!empty($thumb)):?>
<div class="onelist-item-thumb <?php if ($this->options->colorBgPosts == 'defaultColor'): ?> bg-deepgrey<?php elseif ($this->options->colorBgPosts == 'customColor'): ?><?php if (array_key_exists('green',unserialize($this->___fields()))): ?> bg-green<?php elseif (array_key_exists('red',unserialize($this->___fields()))): ?> bg-red<?php elseif (array_key_exists('yellow',unserialize($this->___fields()))): ?> bg-yellow<?php elseif (array_key_exists('blue',unserialize($this->___fields()))): ?> bg-blue<?php elseif (array_key_exists('purple',unserialize($this->___fields()))): ?> bg-purple<?php else : ?> bg-<?php echo randBgColor(); ?><?php endif; ?><?php endif; ?>" style="background-image:url(<?php echo $thumb;?>);"></div>
<?php else : ?>
<div class="onelist-item-thumb <?php if ($this->options->colorBgPosts == 'defaultColor'): ?> bg-deepgrey<?php elseif ($this->options->colorBgPosts == 'customColor'): ?><?php if (array_key_exists('green',unserialize($this->___fields()))): ?> bg-green<?php elseif (array_key_exists('red',unserialize($this->___fields()))): ?> bg-red<?php elseif (array_key_exists('yellow',unserialize($this->___fields()))): ?> bg-yellow<?php elseif (array_key_exists('blue',unserialize($this->___fields()))): ?> bg-blue<?php elseif (array_key_exists('purple',unserialize($this->___fields()))): ?> bg-purple<?php else : ?> bg-<?php echo randBgColor(); ?><?php endif; ?><?php endif; ?>" style="background-image:url(<?php $this->options->themeUrl('images/thumbs/'.mt_rand(0,9).'.jpg'); ?>);"></div>
<?php endif; ?>
<?php endif; ?>
</a>

修改为:

<!-- 单栏文章封面图 -->
<?php $thumb=$this->fields->thumb;?>
<?php if (!empty($thumb)): ?>
<a href="<?php $this->permalink(); ?>">
	<div class="onelist-item-thumb <?php if ($this->options->colorBgPosts == 'defaultColor'): ?> bg-deepgrey<?php elseif ($this->options->colorBgPosts == 'customColor'): ?><?php if (array_key_exists('green',unserialize($this->___fields()))): ?> bg-green<?php elseif (array_key_exists('red',unserialize($this->___fields()))): ?> bg-red<?php elseif (array_key_exists('yellow',unserialize($this->___fields()))): ?> bg-yellow<?php elseif (array_key_exists('blue',unserialize($this->___fields()))): ?> bg-blue<?php elseif (array_key_exists('purple',unserialize($this->___fields()))): ?> bg-purple<?php else : ?> bg-<?php echo randBgColor(); ?><?php endif; ?><?php endif; ?>" style="background-image:url(<?php parseFieldsThumb($this); ?>);"></div>
</a>
<?php endif; ?>

首页设置副标题

调整主题目录下的 header.php 文件,默认在16行-23行的这段 title 标签,在结尾处增加副标题:

<?php if ($this->is('index')): ?> - 副标题<?php endif; ?>

完整代码:

<title><?php $this->archiveTitle(array(
'category'  =>  _t(' %s '),
'search'    =>  _t(' %s '),
'tag'       =>  _t(' %s '),
'author'    =>  _t(' %s '),
'date'      =>  _t(' %s ')
), '', ' - '); ?><?php $this->options->title(); ?><?php if ($this->is('index')): ?> - Blog<?php endif; ?></title>