在 WordPress 主题里引入 Font Awesome 6 图标库

有些主题的图标是引用外部 CDN 的,不稳定。把整套图标库下载到本地再引用,加载更可控。下面记录怎么在主题里接入 Font Awesome 6。

一、下载样式库

先去官网把 Font Awesome 的样式和图标库打包下载下来,官网地址是 https://fontawesome.com/download。

wp_fontawesome6-01

二、上传到服务器

把压缩包解压,目录重命名为 fontawesome,再传到 WordPress 站点所在目录下。比如传到 /opt/wordpress/wp-content/uploads/,后面引用路径按这个来。

三、主题里引用样式

通用做法是在主题的 header.php 里加一条 link,把全部样式一次性引进来:

<!-- 添加 font-awesome 图标 -->
<link href="/wp-content/uploads/fontawesome/css/all.css" rel="stylesheet"> <!--load all styles -->
<!-- END -->
wp_fontawesome6-02

四、向下兼容旧图标

有的主题原来用的是 Font Awesome v4 的图标样式,直接换成 v6 会导致部分旧图标显示不出来。官方给了兼容方案:先引 v6 的核心样式和用到的单个样式(比如 Solid、Brands),再额外引一条 v4 字体面,兼容 v4 的图标写法:

<!-- 添加 font-awesome 图标 -->
<!-- 添加 v6 核心样式,然后选择您需要的单个样式,例如 Solid 和 Brands -->
<link href="/wp-content/uploads/fontawesome/css/fontawesome.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/brands.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/solid.css" rel="stylesheet" />
<!-- 下面5个为v6 Pro版本单独样式 -->
<link href="/wp-content/uploads/fontawesome/css/duotone.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/light.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/regular.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/sharp-solid.css" rel="stylesheet" />
<link href="/wp-content/uploads/fontawesome/css/thin.css" rel="stylesheet" />
<!-- 支持v4图标引用/语法 -->
<link href="/wp-content/uploads/fontawesome/css/v4-font-face.css" rel="stylesheet" />
<!-- END -->

五、免费与付费版本的区别

wp_fontawesome6-03

免费版够用的核心样式是 Solid、Regular、Brands 这几类;Duotone、Light、Thin 以及 sharp 系列属于 Pro 付费版才有的,上面兼容代码里那几条 Pro 的 link 没有对应文件就不用引,否则会报 404。引用路径一定要和实际上传的目录一致。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容