0%

Hexo-github搭建个人博客(4):自定义友情链接页面

1.简介

next主题 的友链,默认是在主题配置文件中 links 下添加,当链接变多以后,侧栏页面的排版很不美观。
这时候就需要给友链新增一个单独的页面了,下面说一下具体步骤.

2.自定义友情链接步骤

2.1 注释主题配置文件的友链

1
2
3
4
5
6
7
8
9
10
# Blog rolls
#links_settings:
# icon: fa fa-link
# title: Links
# # Available values: block | inline
# layout: block

#links:
# links: /links/ || fa fa-link
# 我的CSDN: https://blog.csdn.net/yanxiangyfg

2.2 新增links页面

1
hexo new page links

此命令会在博客根目录 /source 下会生成一个 links 文件夹,打开其中的 index.md 文件,在头部写入 type = “links”,这个一定要写

1
2
3
4
5
---
title: 友情链接
date: 2019-12-08 03:21:39
type: "links"
---

2.3 配置links的menu

在主题配置文件_config.yml的menu中加入:

1
links: /links/ || fa fa-link

再在 /themes/next/languages/zh-Hans.yml 文件中 menu 下增加中文描述

1
links: 友链

2.4 增加友情链接样式

在 /themes/next/layout/ 新建 links.swig,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 {% block content %}
{######################}
{### LINKS BLOCK ###}
{######################}

<div id="links">
<style>

#links{
margin-top: 5rem;
}

.links-content{
margin-top:1rem;
}

.link-navigation::after {
content: " ";
display: block;
clear: both;
}

.card {
width: 300px;
font-size: 1rem;
padding: 10px 20px;
border-radius: 4px;
transition-duration: 0.15s;
margin-bottom: 1rem;
display:flex;
}
.card:nth-child(odd) {
float: left;
}
.card:nth-child(even) {
float: right;
}
.card:hover {
transform: scale(1.1);
box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
.card a {
border:none;
}
.card .ava {
width: 3rem!important;
height: 3rem!important;
margin:0!important;
margin-right: 1em!important;
border-radius:4px;

}
.card .card-header {
font-style: italic;
overflow: hidden;
width: 236px;
}
.card .card-header a {
font-style: normal;
color: #2bbc8a;
font-weight: bold;
text-decoration: none;
}
.card .card-header a:hover {
color: #d480aa;
text-decoration: none;
}
.card .card-header .info {
font-style:normal;
color:#a3a3a3;
font-size:14px;
min-width: 0;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
</style>
<div class="links-content">
<div class="link-navigation">

{% for link in theme.mylinks %}

<div class="card">
<img class="ava" src="{{ link.avatar }}"/>
<div class="card-header">
<div>
<a href="{{ link.site }}" target="_blank"> {{ link.nickname }}</a>
<a href="{{ link.site }}" target="_blank"><span class="focus-links">关注</span></a>
</div>
<div class="info">{{ link.info }}</div>
</div>
</div>

{% endfor %}

</div>
{{ page.content }}
</div>
</div>

{##########################}
{### END LINKS BLOCK ###}
{##########################}
{% endblock %}

2.4.2 修改page.swig页

修改 /themes/next/layout/page.swig 文件,在开头的 block title 内部的{{- __('title.tag') + page_title_suffix }}下方加入友情链接代码,效果如下:

1
2
3
4
5
6
7
 {%- elif page.type === 'tags' and not page.title %}
{{- __('title.tag') + page_title_suffix }}

<!-- 如下两行为新增:友情链接-->
{% elif page.type === 'links' and not page.title %}
{{ __('title.links') + page_title_suffix }}

{% elif page.type === 'categories' %}这个块下方加入links的elif代码块,效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{% elif page.type === 'categories' %}
<div class="category-all-page">
<div class="category-all-title">
{{ _p('counter.categories', site.categories.length) }}
</div>
<div class="category-all">
{{ list_categories() }}
</div>
</div>

<!-- 如下两行为新增:友情链接-->
{% elif page.type === 'links' %}
{% include 'links.swig' %}

2.4.4 配置友情链接

接下来,在 /themes/next/_config.yml 文件中配置友链,末尾处新增 mylinks ,如下:

1
2
3
4
5
6
7
8
9
10
11
mylinks:
- nickname: yanxiangyfg #友链名称
site: https://yanxiangyfg.github.io/ #友链地址
info: 胡杨树:让我们遨游在知识的海洋。 #友链说明
avatar: https://yanxiangyfg.github.io/images/yfg_header_image.png #友链头像

- nickname: yanxiangyfg #友链名称
site: https://blog.csdn.net/yanxiangyfg #友链地址
info: 胡杨树:忠于实践,记录点滴。 #友链说明
avatar: https://yanxiangyfg.github.io/images/yfg_csdn.jfif #友链头像

这里只配置了一个友链,这里是配置了四个友链,多个的配置方式相同.

配置好了之后,hexo clean && hexo g -d 部署看效果吧。

兄弟姐妹们,写文章费脑啊,求打赏点烟钱.