🏗️ 第一步:搭建页面“骨架”(引入 layout.ejs)
现在的 index.ejs 既充当了网页外壳,又写了文章列表,这很乱。标准做法是把“外壳”独立出来。
1. 在 themes/my-theme/layout/ 文件夹里,新建一个文件,叫 layout.ejs。
2. 把下面这段通用骨架代码复制进去(注意 <%- body %> 这一行,这是核心):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title><%= config.title %></title> <link rel="stylesheet" href="/css/style.css"> </head> <body> <header> <h1>欢迎来到我的博客</h1> <nav> <a href="/">首页</a> | <a href="/archives">归档</a> </nav> </header> <main class="container"> <%- body %> </main> <footer> <p>© <%= new Date().getFullYear() %> 我的专属博客</p> </footer> </body> </html>
|
3. 然后,把你现在的 index.ejs 内容清空,只保留最核心的文章列表代码:
<h2>最新文章</h2> <% page.posts.each(function(post){ %> <div class="post-item"> <h3><a href="<%- url_for(post.path) %>"><%= post.title %></a></h3> <p><%= post.excerpt || '暂无摘要' %></p> </div> <% }); %>
|
保存后,在 Git Bash 里跑 hexo clean && hexo s,刷新 localhost:4000。你会发现页面有了页头、导航栏和页脚,文章列表在中间。
🎨 第二步:让页面漂亮起来(加上 CSS)
编写ing
更新时间:2026年9月20日