前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hexo博客 | 如何在博客首页添加公告板模块

Hexo博客 | 如何在博客首页添加公告板模块

作者头像
Justlovesmile
发布2021-12-13 20:53:13
7490
发布2021-12-13 20:53:13
举报
文章被收录于专栏:云+分享云+分享

在我的博客首页添加了告示板模块,有两种模式,一种是自定义语句,一种是一言API

说明

本文是以我现在使用的Ayer主题为例

步骤

第一步 找到对应的首页文章页的代码文章

例如,ayer主题的位于hexo\themes\ayer\layout\_partial\archive.ejs

代码语言:javascript
复制
<section class="outer">
<!-- 找到这里,添加代码 -->
  <% if (theme.broadcast.enable && pagination == 2){ %>
  <%- partial('_partial/broadcast') %>
  <% } %>
<!-- 到这里结束 -->
  <article class="articles">
    <%
    var title = '';
    if (page.category) title = page.category;
    if (page.tag) title = "#" + "&nbsp" + page.tag;
    if (page.archive) {
      if (page.year) title = page.year + (page.month ? '/' + page.month : '');
      else title = __('archive_a');
    }
    %>

第二步 创建broadcast.ejs文件

在第一步里,<%- partial('_partial/broadcast') %>调用了_partial文件夹里面的broadcast.ejs文件,因此需要自己创建一个,内容为:

代码语言:javascript
复制
<% if (theme.broadcast.type===1 && theme.broadcast.text){ %>
<div class="notice" style="margin-top:50px">
    <i class="fa <%- theme.broadcast.icon -%>"></i>
    <div class="notice-content"><%= theme.broadcast.text %></div>
</div>
<% } %>
<% if (theme.broadcast.type===2){ %>
<div class="notice" style="margin-top:50px">
    <i class="fa <%- theme.broadcast.icon -%>"></i>
    <div class="notice-content" id="broad"></div>
</div>
<script type="text/javascript">
   fetch('https://v1.hitokoto.cn')
    .then(response => response.json())
    .then(data => {
      document.getElementById("broad").innerHTML=data.hitokoto;
    })
    .catch(console.error)
</script>
<% } %>
<style>
.notice {
    padding: 20px;
    border: 1px dashed #e6e6e6;
    color: #969696;
    position: relative;
    display: inline-block;
    width: 100%;
    background: #fbfbfb50;
    border-radius: 10px;
}
.notice i{
    float: left;
    color: #999;
    font-size: 18px;
    padding-right: 10px;
    vertical-align: middle;
    margin-top:3px;
}
.notice-content{
    display: initial;
    vertical-align: middle;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">

当然,为了优化,也可以自己把css整合到css文件中,或者把cdn文件放到after-footer里,这里不再详述

第三步 打开主题的配置文件

注意是主题的配置文件!打开后添加配置项:

代码语言:javascript
复制
# 告示板模块
broadcast:
  enable: true   #true开启,false关闭
  icon: fa-bookmark  #fontawesome图标库,格式如示例
  type: 2 #1:自定义输入,2:一言api
  text: justlovesmile.top持续更新中...  #type为1时有效

第四步 效果

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 说明
  • 步骤
    • 第一步 找到对应的首页文章页的代码文章
      • 第二步 创建broadcast.ejs文件
        • 第三步 打开主题的配置文件
          • 第四步 效果
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com