Typecho首页置顶文章的显示问题的解决


主题模板里,本身带有首页文章置顶功能。使用后发现有以下几个问题:

1. 同一篇文章会在首页的所有分类里面均置顶显示;
2. 如果是相同分类下的最近的文章,则会重复显示;

准备开始着手处理这两个问题。

让文章只在所属分类下置顶显示

1.进入主题目录,找到fuctions.php文件,搜索到变量$indextopnum1$indextopnum2的位置,改为:

$indextopnum1 = new Typecho_Widget_Helper_Form_Element_Text('indextopnum1', NULL, NULL, _t('首页置顶某一个特定文章1'), _t('仅用在首页,首页置顶某一个特定id文章,填入<b style="color: red;">id,mid</b>一篇文章及对应分类mid!!!'));
$form->addInput($indextopnum1);    
$indextopnum2 = new Typecho_Widget_Helper_Form_Element_Text('indextopnum2', NULL, NULL, _t('首页置顶某一个特定文章2'), _t('仅用在首页,首页置顶某一个特定id文章,填入<b style="color: red;">id,mid</b>一篇文章及对应分类mid!!!'));
$form->addInput($indextopnum2);

2.登录后台---更改外观---设置外观---首页置顶某一个特定文章1首页置顶某一个特定文章2---填写为“文章ID,分类ID”

3.进入主题目录,找到index.php文件,搜索到indextopnum1indextopnum2的位置。利用explode函数用逗号拆分之前在后台填写的文章ID及分类ID。

<?php if($this->options->indextopnum1): ?>
<?php 
    $archiveTop1 = explode(',', $this->options->indextopnum1);
    $top1Cid = $archiveTop1[0];
    $top1Mid = count($archiveTop1) == 2?$archiveTop1[1]:0
?>
<?php if($top1Mid == 0 || ($categories->mid == $top1Mid)): ?>
<?php $this->widget('Widget_Archive@indextj1', 'pageSize=1&type=post', 'cid='.$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 $indextop->date('Y.n.j'); ?>" itemprop="datePublished"><?php $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 $indextop->permalink(); ?>" target="_blank"><small style="color:Orange;margin-right:5px;">[Top]</small><?php $indextop->title(); ?></a>
    </span>
</li>
<?php endif; ?> 
<?php endif; ?>

<?php if($this->options->indextopnum2): ?>
<?php 
    $archiveTop2 = explode(',', $this->options->indextopnum2);
    $top2Cid = $archiveTop2[0];
    $top2Mid = count($archiveTop2) == 2?$archiveTop2[1]:0
?>
<?php if($top2Mid == 0 || ($categories->mid == $top2Mid)): ?>
<?php $this->widget('Widget_Archive@indextj2', 'pageSize=1&type=post', 'cid='.$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 $indextop->date('Y.n.j'); ?>" itemprop="datePublished"><?php $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 $indextop->permalink(); ?>" target="_blank"><small style="color:Orange;margin-right:5px;">[Top]</small><?php $indextop->title(); ?></a>
    </span>
</li>
<?php endif; ?> 
<?php endif; ?> 

让分类下的文章不再重复显示

进入主题目录,找到index.php文件,搜索到indextopnum1indextopnum2的位置。利用上方已经写好的变量$top1Cid$top2Cid直接做if判断,若cid相同,则不显示文字。

代码紧接着上方的代码继续写

<?php $this->widget('Widget_Archive@index-' . $categories->mid, 'pageSize=12&type=category', 'mid=' . $categories->mid)->to($posts); ?>
<?php while ($posts->next()): ?>
<?php if(($posts->cid != $top1Cid) && ($posts->cid != $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 $posts->date(); ?>" itemprop="datePublished"><?php $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 $posts->permalink(); ?>" target="_blank"><?php $posts->title(); ?></a>
    </span>
</li>
<?php endif; ?>

大功告成

声明:目的地-Destination|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Typecho首页置顶文章的显示问题的解决


前程似锦、未来可期、寻得良人、共赴白头,祝你也祝我。