本文最后更新于 2025年8月7日 下午
首页隐藏功能
参考了 Triority 的帖子 hexo的fluid主题设置文章首页隐藏
我想在首页只放与学习有关的东西,对生活、日记类的帖子设置首页隐藏,而在其他页面正常显示。今天在网上找到了前人的解决方案,但使用之后,发现会导致帖子的首页缩略图无法正常显示。
在仔细检查了他的代码之后,我加以改进,保留了最初文件的 index_img 逻辑,在 md
文件中添加 notshow
字段,并在 index.ejs
文件中的 page.posts.each
循环中添加相应判断条件。
在 md
文件的 front-matter 部分添加 notshow
字段:
1 2 3
| # 是否彻底隐藏/主页显示 hide: false notshow: false
|
修改后的 index.ejs
文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <% if (theme.index.slogan.enable) { page.subtitle = theme.index.slogan.text || config.subtitle || '' } page.banner_img = theme.index.banner_img page.banner_img_height = theme.index.banner_img_height page.banner_mask_alpha = theme.index.banner_mask_alpha %>
<% page.posts.each(function (post) { %> <% if(post.notshow !== true) { %> <!-- 添加notshow判断 --> <div class="row mx-auto index-card"> <% var post_url = url_for(post.path), index_img = post.index_img || theme.post.default_index_img %> <!-- 不做变动,保留原有的index_img逻辑 --> <% if (index_img) { %> ... <!-- 不做变动,保留原有的文章信息展示 --> <% } %> <article class="col-12 col-md-<%= index_img ? '8' : '12' %> mx-auto index-info"> ... <!-- 不做变动,保留原有的文章信息展示 --> </article> </div> <% } %> <!-- 结束notshow判断 --> <% }) %>
<%- partial('_partials/paginator') %>
|
但是还有个问题,首页隐藏的帖子仍然占“幽灵空位”,导致首页显示的文章数目参差不齐,以后再解决吧。