根据原版增加了足迹和memo的页面
指定分类显示
增加文章页面的自定义字段说明
This commit is contained in:
浪子 2025-03-21 11:47:08 +08:00
parent 5dc3ec7059
commit ad347ce0d7
7 changed files with 223 additions and 226 deletions

View File

@ -1,13 +1,5 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('module/header.php'); ?>
<?php
// 获取高亮分类ID
$travelId = Helper::options()->travel;
// 处理可能的情况
$travelId = is_array($travelId)
? intval(reset($travelId))
: intval($travelId);
?>
<header class="archive--header">
<h2 class="post--single__title">
<?php $this->archiveTitle(array(
@ -18,101 +10,33 @@ $travelId = is_array($travelId)
'author' => _t('作者 <span>%s </span>发布的文章')
), '', ''); ?>
</h2>
<?php if ($this->is('category') && $this->categoryDescription()): ?>
<div class="taxonomy--description">
<?php $this->categorydescription(); ?>
<?php echo $this->getDescription(); ?>
</div>
<?php endif; ?>
</header>
<div class="site--main">
<?php
// 获取分类ID配置
$travelId = Helper::options()->travel;
$memosId = Helper::options()->memos;
// 安全地获取当前分类 mid
$currentCategory = isset($this->categories[0]['mid']) ? intval($this->categories[0]['mid']) : null;
// 转换为整型(如果需要)
$travelId = is_numeric($travelId) ? intval($travelId) : null;
$memosId = is_numeric($memosId) ? intval($memosId) : null;
?>
<?php if ($this->have()): ?>
<!-- 高亮分类的文章 -->
<div class="post--cards">
<?php while($this->next()): ?>
<?php
// 检查是否属于高亮分类
$istravel = array_reduce($this->categories, function($carry, $category) use ($travelId) {
return $carry || (intval($category['mid']) === $travelId);
}, false);
if ($istravel):
// 获取文章图片
$default_thumbnail = Helper::options()->themeUrl . '/assets/images/nopic.svg';
$firstImage = img_postthumb($this->cid);
if (empty($firstImage)) {
$firstImage = $default_thumbnail;
}
$cover = $this->fields->cover;
$imageToDisplay = $cover;
if (empty($imageToDisplay)) {
$imageToDisplay = $firstImage;
}
?>
<article class="post--card">
<img src="<?php echo $imageToDisplay; ?>" alt="<?php $this->title() ?>" class="cover" itemprop="image"/>
<div class="content">
<h2 class="post--title">
<a href="<?php $this->permalink() ?>">
<?php $this->title() ?>
</a>
</h2>
<div class="description">
<?php $this->excerpt(20, '...'); ?>
</div>
<div class="meta">
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16">
<path d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z"></path>
</svg>
<time datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time">
<?php $this->date('Y-m-d'); ?>
</time>
</div>
</div>
</article>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- 正常分类的文章 -->
<?php while($this->next()): ?>
<?php
// 检查是否属于高亮分类
$istravel = array_reduce($this->categories, function($carry, $category) use ($travelId) {
return $carry || (intval($category['mid']) === $travelId);
}, false);
if (!$istravel):
?>
<article class="post--item">
<div class="content">
<h2 class="post--title">
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
</h2>
<div class="description">
<?php
// 使用三元运算符简化摘要处理
echo $this->fields->summary
? $this->fields->summary
: $this->excerpt(180);
?>
</div>
<div class="meta">
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16">
<path d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z"></path>
</svg>
<time><?php $this->date('Y-m-d'); ?></time>
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16">
<path d="M408.551619 97.52381a73.142857 73.142857 0 0 1 51.736381 21.430857L539.306667 197.973333A73.142857 73.142857 0 0 0 591.067429 219.428571H804.571429a73.142857 73.142857 0 0 1 73.142857 73.142858v560.761904a73.142857 73.142857 0 0 1-73.142857 73.142857H219.428571a73.142857 73.142857 0 0 1-73.142857-73.142857V170.666667a73.142857 73.142857 0 0 1 73.142857-73.142857h189.123048z m0 73.142857H219.428571v682.666666h585.142858V292.571429h-213.504a146.285714 146.285714 0 0 1-98.499048-38.13181L487.619048 249.734095 408.551619 170.666667zM365.714286 633.904762v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z m-365.714285-195.047619v73.142857h-73.142857v-73.142857h73.142857z m365.714285 0v73.142857H414.47619v-73.142857h316.952381z"></path>
</svg>
<?php $this->category(','); ?>
<svg viewBox="0 0 24 24" class="icon" aria-hidden="true" width="16" height="16">
<g>
<path d="M1.751 10c0-4.42 3.584-8 8.005-8h4.366c4.49 0 8.129 3.64 8.129 8.13 0 2.96-1.607 5.68-4.196 7.11l-8.054 4.46v-3.69h-.067c-4.49.1-8.183-3.51-8.183-8.01zm8.005-6c-3.317 0-6.005 2.69-6.005 6 0 3.37 2.77 6.08 6.138 6.01l.351-.01h1.761v2.3l5.087-2.81c1.951-1.08 3.163-3.13 3.163-5.36 0-3.39-2.744-6.13-6.129-6.13H9.756z"></path>
</g>
</svg>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('0 ', '1 ', '%d '); ?></a>
</div>
</div>
</article>
<?php endif; ?>
<?php endwhile; ?>
<?php if ($currentCategory === $travelId): ?>
<!-- 旅行分类模板 -->
<?php $this->need('module/travel.php'); ?>
<?php elseif ($currentCategory === $memosId): ?>
<!-- 说说分类模板 -->
<?php $this->need('module/memos.php'); ?>
<?php else: ?>
<!-- 默认文章列表 -->
<?php $this->need('module/postlist.php'); ?>
<?php endif; ?>
<!-- 分页导航 -->
<?php $this->pageNav(
' ',
' ',
@ -130,14 +54,8 @@ $travelId = is_array($travelId)
)
); ?>
<?php else: ?>
<main class="site--main">
<header class="archive-header archive-header__search">
<div class="pagination">
<h2>Sorry</h2>
<p>很遗憾,未找到您期待的内容</p>
</div>
</header>
</main>
<?php endif; ?>
<!-- 无结果 -->
<?php $this->need('module/notfound.php'); ?>
<?php endif; ?>
</div>
<?php $this->need('module/footer.php'); ?>

View File

@ -7,8 +7,10 @@ function themeConfig($form) {
$form->addInput($icoUrl);
$sticky = new Typecho_Widget_Helper_Form_Element_Text('sticky', NULL, NULL, _t('置顶文章cid'), _t('多篇文章以`|`符号隔开'), _t('会在首页展示置顶文章。'));
$form->addInput($sticky);
$travel = new Typecho_Widget_Helper_Form_Element_Text('travel', NULL, '3', _t('travel分类 Mid'), _t('多个分类以`|`符号隔开'), _t('指定分类ID用于区别展示'));
$travel = new Typecho_Widget_Helper_Form_Element_Text('travel', NULL, NULL, _t('travel分类 Mid'), _t('填写分类的mid'), _t('指定分类ID用于足迹分类展示'));
$form->addInput($travel);
$memos = new Typecho_Widget_Helper_Form_Element_Text('memos', NULL, NULL, _t('说说分类 Mid'), _t('填写分类的mid'), _t('指定分类ID用于说说分类展示'));
$form->addInput($memos);
$instagramurl = new Typecho_Widget_Helper_Form_Element_Text('instagramurl', NULL, 'https://Instagram.com/', _t('Instagram'), _t('会在个人信息显示'));
$form->addInput($instagramurl);
$telegramurl = new Typecho_Widget_Helper_Form_Element_Text('telegramurl', NULL, 'https://t.me/', _t('电报'), _t('会在个人信息显示'));
@ -62,6 +64,24 @@ function saveThemeConfig($config) {
// 可以在这里添加额外的验证或处理逻辑
return $config;
}
function themeFields($layout) {
$summary= new Typecho_Widget_Helper_Form_Element_Textarea('summary', NULL, NULL, _t('文章摘要'), _t('自定义摘要'));
$layout->addItem($summary);
$cover= new Typecho_Widget_Helper_Form_Element_Text('cover', NULL, NULL, _t('文章封面'), _t('自定义文章封面'));
$layout->addItem($cover);
$douban= new Typecho_Widget_Helper_Form_Element_Text('douban', NULL, NULL, _t('豆瓣API'), _t('自定义页面豆瓣API'));
$layout->addItem($douban);
$neodb= new Typecho_Widget_Helper_Form_Element_Text('neodb', NULL, NULL, _t('NeoDB API'), _t('自定义页面NeoDB API'));
$layout->addItem($neodb);
$tooot= new Typecho_Widget_Helper_Form_Element_Text('tooot', NULL, NULL, _t('Mastodon API'), _t('自定义页面Mastodon API'));
$layout->addItem($tooot);
$memos= new Typecho_Widget_Helper_Form_Element_Text('memos', NULL, NULL, _t('Memos地址'), _t(' 自定义页面Memos地址'));
$layout->addItem($memos);
$memosID= new Typecho_Widget_Helper_Form_Element_Text('memosID', NULL, NULL, _t('Memos ID'), _t('自定义页面Memos ID'));
$layout->addItem($memosID);
$memosnum= new Typecho_Widget_Helper_Form_Element_Text('memosnum', NULL, NULL, _t('Memos数量'), _t('自定义页面Memos数量'));
$layout->addItem($memosnum);
}
/*
* 文章浏览数统计
*/

115
index.php
View File

@ -6,12 +6,125 @@
* @version 0.7.0
* @link https://www.imsun.org
*/
// 确保退出安全
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('module/header.php');
/** 文章置顶 */
$sticky = $this->options->sticky; // 置顶的文章id多个用|隔开
$db = Typecho_Db::get();
$pageSize = $this->options->pageSize;
if ($sticky && !empty(trim($sticky))) {
$sticky_cids = array_filter(explode('|', $sticky)); // 分割文本并过滤空值
if (!empty($sticky_cids)) {
$sticky_html = " <span class='sticky--post'> 置顶 </span> "; // 置顶标题的 html
// 清空原有文章的队列
$this->row = [];
$this->stack = [];
$this->length = 0;
// 获取总数,并排除置顶文章数量
if (isset($this->currentPage) && $this->currentPage == 1) {
$totalOriginal = $this->getTotal();
$stickyCount = count($sticky_cids);
$this->setTotal(max($totalOriginal - $stickyCount, 0));
// 构建置顶文章的查询
$selectSticky = $this->select()->where('type = ?', 'post');
foreach ($sticky_cids as $i => $cid) {
if ($i == 0)
$selectSticky->where('cid = ?', $cid);
else
$selectSticky->orWhere('cid = ?', $cid);
}
// 获取置顶文章
$stickyPosts = $db->fetchAll($selectSticky);
// 压入置顶文章到文章队列
foreach ($stickyPosts as &$stickyPost) {
$stickyPost['title'] .= $sticky_html;
$this->push($stickyPost);
}
$standardPageSize = $pageSize - count($stickyPosts);
} else {
$standardPageSize = $pageSize;
}
// 构建正常文章查询,排除置顶文章
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $standardPageSize);
foreach ($sticky_cids as $cid) {
$selectNormal->where('table.contents.cid != ?', $cid);
}
} else {
// 如果sticky_cids为空使用默认查询
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize);
}
} else {
// 如果没有置顶文章,使用默认查询
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize);
}
// 登录用户显示私密文章
if ($this->user->hasLogin()) {
$uid = $this->user->uid;
if ($uid) {
$selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private');
}
}
$normalPosts = $db->fetchAll($selectNormal);
// 如果之前没有清空队列(没有置顶文章的情况),现在清空
if (empty($sticky) || empty(trim($sticky)) || empty($sticky_cids)) {
$this->row = [];
$this->stack = [];
$this->length = 0;
}
// 压入正常文章到文章队列
foreach ($normalPosts as $normalPost) {
$this->push($normalPost);
}
?>
<?php $this->need('module/header.php');?>
<main class="site--main">
<div class="articleList">
<?php $this->need('module/postlist.php'); ?>
<?php if ($this->options->loadmore): ?>
<?php
$this->pageNav(
' ',
' ',
1,
'...',
array(
'wrapTag' => 'nav',
'wrapClass' => 'nav-links nav-links__comment',
'itemTag' => '',
'textTag' => 'span',
'itemClass' => 'page-numbers',
'currentClass' => 'page-numbers current',
'prevClass' => 'hidden',
'nextClass' => 'hidden'
)
);
?>
<?php else:?>
<?php
$nextPage = $this->_currentPage + 1;
$totalPages = ceil($this->getTotal() / $this->parameter->pageSize);
if ($this->_currentPage < $totalPages): ?>
<div class="nav-links">
<span class="loadmore"><?php $this->pageLink('加载更多', 'next'); ?></span>
</div>
<?php endif; ?>
<script src="<?php $this->options->themeUrl('assets/js/loadmore.js'); ?>"></script>
<?php endif; ?>
</div>
</main>
<?php $this->need('module/footer.php'); ?>

17
module/memos.php Normal file
View File

@ -0,0 +1,17 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php while($this->next()): ?>
<article class="post--item post--item__status" itemtype="http://schema.org/Article" itemscope="itemscope">
<div class="content">
<header>
<img src="<?php $this->options->logoUrl() ?>" class="avatar" width="48" height="48" />
<a datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time" href="<?php $this->permalink() ?>"
itemprop="datePublished">
<?php $this->date('Y-m-d'); ?>
</a>
</header>
<div class="description" itemprop="about">
<?php $this->excerpt(200, '...'); ?>
</div>
</div>
</article>
<?php endwhile; ?>

8
module/notfound.php Normal file
View File

@ -0,0 +1,8 @@
<main class="site--main">
<header class="archive-header archive-header__search">
<div class="pagination">
<h2>Sorry</h2>
<p>很遗憾,未找到您期待的内容</p>
</div>
</header>
</main>

View File

@ -1,89 +1,3 @@
<?php
// 确保退出安全
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/** 文章置顶 */
$sticky = $this->options->sticky; // 置顶的文章id多个用|隔开
$db = Typecho_Db::get();
$pageSize = $this->options->pageSize;
if ($sticky && !empty(trim($sticky))) {
$sticky_cids = array_filter(explode('|', $sticky)); // 分割文本并过滤空值
if (!empty($sticky_cids)) {
$sticky_html = " <span class='sticky--post'> 置顶 </span> "; // 置顶标题的 html
// 清空原有文章的队列
$this->row = [];
$this->stack = [];
$this->length = 0;
// 获取总数,并排除置顶文章数量
if (isset($this->currentPage) && $this->currentPage == 1) {
$totalOriginal = $this->getTotal();
$stickyCount = count($sticky_cids);
$this->setTotal(max($totalOriginal - $stickyCount, 0));
// 构建置顶文章的查询
$selectSticky = $this->select()->where('type = ?', 'post');
foreach ($sticky_cids as $i => $cid) {
if ($i == 0)
$selectSticky->where('cid = ?', $cid);
else
$selectSticky->orWhere('cid = ?', $cid);
}
// 获取置顶文章
$stickyPosts = $db->fetchAll($selectSticky);
// 压入置顶文章到文章队列
foreach ($stickyPosts as &$stickyPost) {
$stickyPost['title'] .= $sticky_html;
$this->push($stickyPost);
}
$standardPageSize = $pageSize - count($stickyPosts);
} else {
$standardPageSize = $pageSize;
}
// 构建正常文章查询,排除置顶文章
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $standardPageSize);
foreach ($sticky_cids as $cid) {
$selectNormal->where('table.contents.cid != ?', $cid);
}
} else {
// 如果sticky_cids为空使用默认查询
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize);
}
} else {
// 如果没有置顶文章,使用默认查询
$selectNormal = $this->select()
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created < ?', time())
->order('created', Typecho_Db::SORT_DESC)
->page(isset($this->currentPage) ? $this->currentPage : 1, $pageSize);
}
// 登录用户显示私密文章
if ($this->user->hasLogin()) {
$uid = $this->user->uid;
if ($uid) {
$selectNormal->orWhere('authorId = ? AND status = ?', $uid, 'private');
}
}
$normalPosts = $db->fetchAll($selectNormal);
// 如果之前没有清空队列(没有置顶文章的情况),现在清空
if (empty($sticky) || empty(trim($sticky)) || empty($sticky_cids)) {
$this->row = [];
$this->stack = [];
$this->length = 0;
}
// 压入正常文章到文章队列
foreach ($normalPosts as $normalPost) {
$this->push($normalPost);
}
?>
<?php while($this->next()): ?>
<article class="post--item">
<div class="content">
@ -149,34 +63,4 @@ foreach ($normalPosts as $normalPost) {
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php if ($this->options->loadmore): ?>
<?php
$this->pageNav(
' ',
' ',
1,
'...',
array(
'wrapTag' => 'nav',
'wrapClass' => 'nav-links nav-links__comment',
'itemTag' => '',
'textTag' => 'span',
'itemClass' => 'page-numbers',
'currentClass' => 'page-numbers current',
'prevClass' => 'hidden',
'nextClass' => 'hidden'
)
);
?>
<?php else:?>
<?php
$nextPage = $this->_currentPage + 1;
$totalPages = ceil($this->getTotal() / $this->parameter->pageSize);
if ($this->_currentPage < $totalPages): ?>
<div class="nav-links">
<span class="loadmore"><?php $this->pageLink('加载更多', 'next'); ?></span>
</div>
<?php endif; ?>
<script src="<?php $this->options->themeUrl('assets/js/loadmore.js'); ?>"></script>
<?php endif; ?>
<?php endwhile; ?>

37
module/travel.php Normal file
View File

@ -0,0 +1,37 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php // 获取文章图片
$default_thumbnail = Helper::options()->themeUrl . '/assets/images/nopic.svg';
$firstImage = img_postthumb($this->cid);
if (empty($firstImage)) {
$firstImage = $default_thumbnail;
}
$cover = $this->fields->cover;
$imageToDisplay = $cover;
if (empty($imageToDisplay)) {
$imageToDisplay = $firstImage;
}
?>
<div class="post--cards">
<?php while($this->next()): ?>
<article class="post--card">
<img src="<?php echo $imageToDisplay; ?>" alt="<?php $this->title() ?>" class="cover" itemprop="image"/>
<div class="content">
<h2 class="post--title">
<a href="<?php $this->permalink() ?>">
<?php $this->title() ?>
</a>
</h2>
<div class="description">
<?php $this->excerpt(20, '...'); ?>
</div>
<div class="meta">
<svg class="icon" viewBox="0 0 1024 1024" width="16" height="16">
<path d="M512 97.52381c228.912762 0 414.47619 185.563429 414.47619 414.47619s-185.563429 414.47619-414.47619 414.47619S97.52381 740.912762 97.52381 512 283.087238 97.52381 512 97.52381z m0 73.142857C323.486476 170.666667 170.666667 323.486476 170.666667 512s152.81981 341.333333 341.333333 341.333333 341.333333-152.81981 341.333333-341.333333S700.513524 170.666667 512 170.666667z m36.571429 89.697523v229.86362h160.865523v73.142857H512a36.571429 36.571429 0 0 1-36.571429-36.571429V260.388571h73.142858z"></path>
</svg>
<time datetime='<?php $this->date('Y-m-d'); ?>' class="humane--time">
<?php $this->date('Y-m-d'); ?>
</time>
</div>
</div>
</article>
<?php endwhile; ?>