Typecho非插件实现首页隐藏单个或多个分类的文章

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

思路大概是:

  • 在模板中添加设置项->欲隐藏的分类ID(用英文逗号分割取多个)
  • index.php重新给文章列队(置顶文章同理)。
  • 最后通过分类与文章的关系(typecho文章分类是另外一个表)进行分类筛选。

以下是index.php加入的代码:

    //首页隐藏某分类文章
    if($this->options->cidId && $this->is('index')){
        $this->row = [];
        $this->stack = [];
        $this->length = 0;
        $order = ''; //清空文章队列
        $db = Typecho_Db::get();
        $restPostSelect = $this->select('table.contents.cid', 'table.contents.title', 'table.contents.slug', 'table.contents.created', 'table.contents.authorId','table.contents.modified', 'table.contents.type', 'table.contents.status', 'table.contents.text', 'table.contents.commentsNum', 'table.contents.order','table.contents.template', 'table.contents.password', 'table.contents.allowComment', 'table.contents.allowPing', 'table.contents.allowFeed','table.contents.parent')->where('table.contents.type = ? and table.contents.status = ? and table.contents.created < ?',
        'post','publish',time())->group('table.contents.cid');
        $restPostSelect = $restPostSelect->join('table.relationships','table.relationships.cid = table.contents.cid','right')->join('table.metas','table.relationships.mid = table.metas.mid','right')->where('table.metas.type=?','category');
        $cidId = explode(',', $this->options->cidId);//分割文本
        $cidcount = 0;
        foreach($cidId as $i => $cid) {
                $cidcount = $cidcount + count($db->fetchAll($db->select('cid')->from('table.relationships')->where('mid = '.intval($cid))));
            $restPostSelect->where('table.relationships.mid != '.intval($cid))->group('cid');
        }
        $endSelect = $restPostSelect->order('table.contents.created', Typecho_Db::SORT_DESC);
        $rest_posts = $db->fetchAll($restPostSelect->order('table.contents.created', Typecho_Db::SORT_DESC)->page($this->_currentPage, $this->parameter->pageSize));
        foreach($rest_posts as $rest_post) {
            $this->push($rest_post);
        }
        $this->setTotal($this->getTotal()-count($cidcount));//重新设置文章数
    }

在functions.php同样要加模板设置

$cidId = new Typecho_Widget_Helper_Form_Element_Text('cidId', NULL, NULL, _t('首页列表不显示的分类ID'), _t('在这里填入欲隐藏的分类ID,使用半角逗号“,”填入多个,如:1,2,留空不显示'));
    $form->addInput($cidId);
0
EMLOG调用正文中图片(多图模式)的方法
« 上一篇 01-22
Typecho如何获取文章图片数量?
下一篇 » 01-24

评论 (1)

插入图片
  1. 头像
    绎泽 Lv.1   安徽省安庆市
    Android · Google Chrome
    沙发

    代码要加在index.php的哪个部分呀,我也是joe主题

    回复