Typecho首页置顶文章的显示问题的解决
**主题模板里,本身带有首页文章置顶功能。使用后发现有以下几个问题:**
1. 同一篇文章会在首页的所有分类里面均置顶显示; 2. 如果是相同分类下的最近的文章,则会重复显示;

准备开始着手处理这两个问题。
让文章只在所属分类下置顶显示
1.进入主题目录,找到fuctions.php文件,搜索到变量<!--INLINEMATH0-->indextopnum2的位置,改为:
$indextopnum1 = new Typecho_Widget_Helper_Form_Element_Text('indextopnum1', NULL, NULL, _t('首页置顶某一个特定文章1'), _t('仅用在首页,首页置顶某一个特定id文章,填入<b style="color: red;">id,mid</b>一篇文章及对应分类mid!!!'));
<!--INLINEMATH1-->indextopnum1);
$indextopnum2 = new Typecho_Widget_Helper_Form_Element_Text('indextopnum2', NULL, NULL, _t('首页置顶某一个特定文章2'), _t('仅用在首页,首页置顶某一个特定id文章,填入<b style="color: red;">id,mid</b>一篇文章及对应分类mid!!!'));
<!--INLINEMATH2-->indextopnum2);
2.登录后台—更改外观—设置外观—首页置顶某一个特定文章1或首页置顶某一个特定文章2—填写为“文章ID,分类ID”

3.进入主题目录,找到index.php文件,搜索到indextopnum1或indextopnum2的位置。利用explode函数用逗号拆分之前在后台填写的文章ID及分类ID。
<?php if($this->options->indextopnum1): ?>
<?php
<!--INLINEMATH3-->this->options->indextopnum1);
<!--INLINEMATH4-->archiveTop1[0];
<!--INLINEMATH5-->archiveTop1) == 2?$archiveTop1[1]:0
?>
<?php if(<!--INLINEMATH6-->categories->mid == $top1Mid)): ?>
<?php <!--INLINEMATH7-->top1Cid.'')->to($indextop); ?>
<li class="post-item" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:100%;">
<div class="meta"><time datetime="<?php <!--INLINEMATH8-->indextop->date('Y.n.j'); ?></time>
</div>
<span style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:95%;"><a href="<?php <!--INLINEMATH9-->indextop->title(); ?></a>
</span>
</li>
<?php endif; ?>
<?php endif; ?>
<?php if($this->options->indextopnum2): ?>
<?php
<!--INLINEMATH10-->this->options->indextopnum2);
<!--INLINEMATH11-->archiveTop2[0];
<!--INLINEMATH12-->archiveTop2) == 2?$archiveTop2[1]:0
?>
<?php if(<!--INLINEMATH13-->categories->mid == $top2Mid)): ?>
<?php <!--INLINEMATH14-->top2Cid.'')->to($indextop); ?>
<li class="post-item" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:100%;">
<div class="meta"><time datetime="<?php <!--INLINEMATH15-->indextop->date('Y.n.j'); ?></time>
</div>
<span style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:95%;"><a href="<?php <!--INLINEMATH16-->indextop->title(); ?></a>
</span>
</li>
<?php endif; ?>
<?php endif; ?>
让分类下的文章不再重复显示
进入主题目录,找到index.php文件,搜索到indextopnum1或indextopnum2的位置。利用上方已经写好的变量<!--INLINEMATH17-->top2Cid直接做if判断,若cid相同,则不显示文字。
代码紧接着上方的代码继续写
<?php <!--INLINEMATH18-->categories->mid, 'pageSize=12&type=category', 'mid=' . <!--INLINEMATH19-->posts); ?>
<?php while ($posts->next()): ?>
<?php if((<!--INLINEMATH20-->top1Cid) && (<!--INLINEMATH21-->top2Cid)): ?>
<li class="post-item" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:100%;">
<div class="meta"><time datetime="<?php <!--INLINEMATH22-->posts->date('Y.n.j'); ?></time>
</div>
<span style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;word-break: break-all;max-width:95%;"><a href="<?php <!--INLINEMATH23-->posts->title(); ?></a>
</span>
</li>
<?php endif; ?>
大功告成

15 个赞
