Hugo添加友链页面

友链是博客扩大影响力的好工具,本文介绍如何为Hugo博客添加友链页面。

步骤

内联 html 实现

配置友链页面链接在《Hugo搭建个人博客(2)》的config.yml中有提到,对应如下配置:

1
2
3
4
          - identifier: links
            name: 🤝友链
            url: links
            weight: 60

为了保持简单,便于修改,可以内联 html 实现。

编辑content/links.md,添加如下内容:

 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
---
title: "🤝友链"
layout: links
summary: links
---

<style>
.friend-links {
  display: flex;
  justify-content: space-between; /* 让元素均匀分布在一排 */
  flex-wrap: wrap; /* 允许元素换行 */
}

.friend-link {
  display: inline-block;
  margin: 5%;
  text-align: center;
  text-decoration: none;
  color: var(--content);
  max-width: 15%;
}

.friend-link img {
  width: 100%;
  height: auto;
  border-radius: 50%;
}

.friend-link .name {
  margin-top: 5px;
}
</style>

<div class="friend-links">
  <a class="friend-link" href="https://333rd.net/" target="_blank">
    <img src="https://333rd.net/img/logo.gif" alt="友链LOGO" loading="lazy">
    <div class="name">3rd's Blog</div>
  </a>
  <!-- 添加更多友链元素 -->
</div>

然后执行hugo --gc更新博客即可。

Shortcodes 实现

如果用短代码Shortcodes实现,可以参考《为Hugo搭建的博客添加友情链接,顺利和LINUX DO交换友链

新建layouts/shortcodes/friendlink.html,添加如下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<article class="friend-card">
  <a class="friend-link" href="{{ .Get "url" }}" target="_blank">
    <div class="friend-logo">
      <img src="{{ .Get "logo" }}" alt="友链LOGO" loading="lazy">
    </div>
    <div class="friend-info">
      <div class="friend-name">{{ .Get "name" }}</div>
      <div class="friend-slogan">{{ .Get "slogan" }}</div>
    </div>
  </a>
</article>

新建assets/css/extended/links.css

 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
.friend-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin: 20px 0;
}

.friend-card {
  flex: 0 1 200px;
  border: 1px solid #ccc;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  min-height: 250px; /* 确保卡片高度一致 */
}

.friend-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.friend-link {
  display: block;
  text-decoration: none;
  color: var(--content);
  padding: 15px;
  text-align: center;
}

.friend-logo {
  width: 100%;
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f7f7f7;
}

.friend-logo img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transition: opacity 0.3s ease;
}

.friend-logo:hover img {
  opacity: 0.8;
}

.friend-info {
  padding: 10px;
  text-align: center;
  min-height: 100px; /* 确保有足够空间容纳slogan */
}

.friend-name {
  font-size: 18px;
  font-weight: bold;
  margin: 5px 0;
}

.friend-slogan {
  font-size: 14px;
  color: #666;
  margin: 0;
}

@media (max-width: 768px) {
  .friend-card {
    flex: 0 1 150px;
  }
  
  .friend-logo {
    height: 120px;
  }
  
  .friend-name {
    font-size: 16px;
  }
  
  .friend-slogan {
    font-size: 12px;
  }
}

新建static/friendlogo/,友链LOGO可以放到这里。

最后在content/links.md中通过如下方式使用短代码新增友链:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
---
title: "🤝友链"
layout: links
summary: links
---

<!--友链需要在此标签内使用以配合CSS样式-->
<div class="friend-container">

{{< friendlink name="3rd's Blog" url="https://333rd.net/" logo="/friendlogo/3rd.logo.png" slogan="Slogan占位符,未来可能会使用。" >}}

</div>

引用