Adding a Friend Links Page to Hugo#
Friend links are a great tool for expanding a blog’s reach. This article explains how to add a friend links page to a Hugo blog.
Steps#
Inline HTML Implementation#
Configuring the friend links page link is mentioned in config.yml in Building a Personal Blog with Hugo (2), corresponding to the following configuration:
1
2
3
4
| - identifier: links
name: 🤝Friend Links
url: links
weight: 60
|
To keep it simple and easy to modify, it can be implemented with inline HTML.
Edit content/links.md and add the following content:
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: "🤝Friend Links"
layout: links
summary: links
---
<style>
.friend-links {
display: flex;
justify-content: space-between; /* Distribute elements evenly in a row */
flex-wrap: wrap; /* Allow elements to 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="Friend Link Logo" loading="lazy">
<div class="name">3rd's Blog</div>
</a>
<!-- Add more friend link elements -->
</div>
|
Then run hugo --gc to update your blog.
Shortcodes Implementation#
If you want to implement it with Shortcodes, you can refer to《Adding friend links to a Hugo blog and successfully exchanging friend links with LINUX DO》.
Create layouts/shortcodes/friendlink.html and add the following content:
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="Friend Link 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>
|
Create 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; /* Ensure consistent card height */
}
.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; /* Ensure enough space for the 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;
}
}
|
Create static/friendlogo/ — friend link logos can be placed here.
Finally, in content/links.md, use the shortcode as follows to add friend links:
1
2
3
4
5
6
7
8
9
10
11
12
| ---
title: "🤝Friend Links"
layout: links
summary: links
---
<!--Friend links need to be used inside this tag to match the CSS styles-->
<div class="friend-container">
{{< friendlink name="3rd's Blog" url="https://333rd.net/" logo="/friendlogo/3rd.logo.png" slogan="Slogan placeholder; may be used in the future." >}}
</div>
|
References#