wordpress谷歌字体

Google fonts are amazing free resource for web designers. In WPBv4, we have started using a popular Google Font combination: Oswald and Lora. Some of our users have asked us how to add Google Web fonts in WordPress themes. If you remember, we showed how to add Google fonts in WordPress Post Editor. In this article, we will show you how to add Google Web Fonts in your WordPress themes the RIGHT way, optimized for performance.

Google字体是Web设计人员的绝佳免费资源。 在WPBv4中 ,我们开始使用一种流行的Google字体组合:Oswald和Lora。 我们的一些用户问我们如何在WordPress主题中添加Google Web字体。 如果您还记得,我们展示了如何在WordPress Post Editor中添加Google字体 。 在本文中,我们将向您展示如何以正确的方式在WordPress主题中添加Google Web字体,并对其性能进行了优化。

查找您喜欢的Google网络字体 (Find the Google Web Fonts that You Like)

First thing you need to do is find a Google font that you like. Head on over to Google fonts and browse through the library. When you find the font that you like, click on the “Quick-use” button.

您需要做的第一件事就是找到您喜欢的Google字体。 转到Google字体并浏览库。 找到所需的字体后,单击“快速使用”按钮。

Once you click the quick-use button, you will be taken to a new page. Scroll down till you see the usage instruction box with code that you can add to your website.

单击快速使用按钮后,将带您进入新页面。 向下滚动,直到看到用法说明框,其中包含可以添加到网站的代码。

You will see that there are three different tabs for adding the font to your site. The first one is the standard and recommended method to add Google fonts to your site. The second tab uses the @import CSS method, and the last tab utilizes the JavaScript method.

您将看到有三个不同的选项卡,用于将字体添加到您的站点。 第一种是将Google字体添加到您的网站的标准和推荐方法。 第二个选项卡使用@import CSS方法,最后一个选项卡使用JavaScript方法。

We will show you how to use each of these methods and what are there pros and cons.

我们将向您展示如何使用这些方法中的每一种以及优点和缺点。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

在WordPress主题中添加Google Web字体 (Adding Google Web Fonts in WordPress Themes)

We have mostly seen folks using the first two methods.

我们通常看到人们使用前两种方法。

The easiest way would be to open your theme’s style.css file and paste the fonts code that you got in the @import tab, like this:

最简单的方法是打开主题的style.css文件并粘贴在@import选项卡中获得的字体代码,如下所示:


@import url(https://fonts.googleapis.com/css?family=Lora);
@import url(https://fonts.googleapis.com/css?family=Oswald);

You can also combine multiple font requests into one. Here is how you would do it:

您还可以将多个字体请求组合为一个。 这是您的操作方式:

@import url(https://fonts.googleapis.com/css?family=Lora|Oswald);

This method is super easy but it is not the best way add Google fonts to your WordPress site. Using @import method blocks parallel downloads, which means the browser will wait for the imported file to finish downloading before it starts downloading the rest of the content.

这种方法超级简单,但不是将Google字体添加到WordPress网站的最佳方法。 使用@import方法会阻止并行下载,这意味着浏览器将等待导入的文件完成下载,然后才开始下载其余内容。

If you MUST use @import, then at least combine multiple requests into one.

如果必须使用@import,则至少将多个请求合并为一个。

性能优化的添加Google Web字体的方法 (Performance Optimized Method of Adding Google Web Fonts)

The best way of adding Google fonts is by using the Standard method which utilizes the link method instead of the import method. Simply take your font URL that you got from step 1. If you are adding multiple fonts, then you can combine the two fonts with a | character. Then place the code in your theme’s head section.

添加Google字体的最佳方法是使用标准方法,该方法利用链接方法而不是导入方法。 只需使用从步骤1中获得的字体URL。如果要添加多个字体,则可以将两个字体与|组合。 字符。 然后将代码放在主题的头部。

You will most likely have to edit your header.php file, and paste the following code above your main stylesheet. The example would look like this:

您很可能必须编辑header.php文件,并将以下代码粘贴到主样式表上方。 该示例如下所示:


<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Lora|Oswald" media="screen">
<link rel="stylesheet" type="text/css" href="YOUR THEME STYLESHEET" media="screen">

Basically the goal is to put the font request as early as possible. According to the Google Web Fonts blog, if there is a script tag before the @font-face declaration, then Internet Explorer won’t render anything on the page until the font file is done downloading.

基本上,目标是尽早提出字体请求。 根据Google Web Fonts博客的说法,如果在@ font-face声明之前有一个脚本标记,则Internet Explorer在字体文件下载完成之前不会在页面上呈现任何内容。

Once you have done that, you can simply start using it in your theme’s CSS file like this:

完成此操作后,您可以像这样在主题CSS文件中开始使用它:

h1 {
font-family: 'Oswald', Helvetica, Arial, serif;
}

Now there are a lot of theme frameworks and child themes out there. It is NOT recommended to modify your parent theme’s files specially if you are using a theme framework because your changes will be overridden the next time you update that framework. You will need to utilize the hooks and filters presented to you by that parent theme or framework to add Google fonts properly in your child themes.

现在有很多主题框架和子主题 。 如果使用主题框架,则不建议您特别修改父主题的文件,因为下次您更新该框架时,所做的更改将被覆盖。 您将需要利用该父主题或框架提供给您的挂钩和过滤器,以在您的子主题中正确添加Google字体。

在WordPress中适当地使Google字体入队 (Properly Enqueuing Google Fonts in WordPress)

Another way to add Google fonts to your WordPress site is by enqueuing the font in your theme’s functions.php file or a site specific plugin.

将Google字体添加到WordPress网站的另一种方法是将字体放入主题的functions.php文件或特定于站点的插件中 。


function wpb_add_google_fonts() {
wp_enqueue_style( 'wpb-google-fonts', 'https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,700italic,400,700,300', false );
}
add_action( 'wp_enqueue_scripts', 'wpb_add_google_fonts' );

Don’t forget to replace the font link with your own.

不要忘记用自己的字体替换字体链接。

使用JavaScript加载Google字体 (Loading Google Fonts Using JavaScript)

For this method you will need to copy the code in the JavaScript tab in Google fonts usage instructions section. You can paste this code in your theme or child theme’s header.php file immediately after <head> tag.

对于这种方法,您需要将代码复制到Google字体使用说明部分JavaScript标签中。 您可以在<head>标记后立即将此代码粘贴到主题或子主题的header.php文件中。

Our last tip on using Google Web Fonts on your site would be to don’t load fonts you won’t use. For example, if you only want the bold, and normal weight, then don’t add all the other styles.

关于在您的网站上使用Google网络字体的最后一个技巧是不要加载不会使用的字体。 例如,如果您只想要粗体和正常的粗细,则不要添加所有其他样式。

We hope that this article helped you add Google Web Fonts in your WordPress themes the right way, so your site can load fast. You may also want to check out our guide on how to add Typekit fonts in WordPress.

我们希望本文能帮助您以正确的方式在WordPress主题中添加Google Web字体,以便您的网站能够快速加载。 您可能还需要查看有关如何在WordPress中添加Typekit字体的指南。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/how-add-google-web-fonts-wordpress-themes/

wordpress谷歌字体

wordpress谷歌字体_如何以“正确”的方式在WordPress主题中添加Google Web字体相关推荐

  1. wordpress谷歌字体_如何在WordPress中添加Google Maps

    wordpress谷歌字体 You can add all kind of content into your WordPress site. At WPBeginner we have shown ...

  2. 谷歌字体_优化Google Web字体

    Google Web字体是在网站上嵌入自定义字体的较流行的方法之一. 它是免费的,并且库中有很多选项可供选择. 尽管可以通过快速的Google服务器提供服务,但仍有一些地方需要改进. 因此,让我们来看 ...

  3. 如何在WordPress中添加Google Maps Store Locator

    Do you want to add Google Maps store locator in WordPress? A store locator is a map pointing to your ...

  4. Android中添加思源字体/NotoSansCJK/SourceHanSans

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 系统版本 ...

  5. 超棒的Google web字体查询工具 - Best Web Fonts

    日期:2012-9-30  来源:GBin1.com Best web fonts是一个免费的字体工具,能够帮助你直观清晰的查询Google的web字体,你可以在一个现成的网站中调试不同类型的Goog ...

  6. 大前端Dux主题如何添加导航菜单字体图标

    对于刚刚接触大前端Dux主题的小伙伴来说,想要在短时间内折腾出一个漂亮大气的网站博客,也并不是那么容易,因为大前端Dux主题的功能真的很多很多,你自己都不知道从那里下手.今天跟小伙伴们说说大前端Dux ...

  7. Google Web 字体 API 访谈

    对很多纠结于字体的 Web 设计与开发者而言,昨天有关 Google Font API 的新 闻 着实让他们高兴了一回,这个非常简单实用的 API 包含了一套字体库 和预览工具 ,结合 Google ...

  8. wordpress谷歌字体_如何在WordPress帖子编辑器中添加Google字体

    wordpress谷歌字体 Fonts are one of the most important aspects of your WordPress blog, and in case you di ...

  9. wordpress最佳架构_汽车经销商的21个最佳WordPress主题

    wordpress最佳架构 Are you looking for the best WordPress themes for car dealerships? 您是否在寻找适用于汽车经销商的最佳Wo ...

最新文章

  1. 彻底搞懂感受野的含义与计算
  2. WebAssembly 的由来
  3. GOCN每日新闻(2017-08-12)
  4. TCP 端口监听队列原理
  5. php 降低图像大小,PHP图像重新调整大小
  6. 修改npm安装的全局路径和配置环境变量的坑
  7. 第k小的数(二分、partition)
  8. python的flask微服务-flask微服务框架的初步接触
  9. leetcode : Rotate List
  10. inceptor手册
  11. 华为员工去面试被淘汰后怒怼HR:华为出来的也能被拒,很无语
  12. excel技巧——F9键
  13. 通信原理SNR EbN0 EsN0理解
  14. python处理can协议文件_二、如何解决:python:Can't reopen .pyc file
  15. 【Cesium】【vue】空间查询——量距(测量距离)、量面(测量面积)
  16. CF1287B CF567C
  17. ubuntu 彻底删除php7,Ubuntu彻底删除PHP7.0的方法
  18. linux终端联网网速慢,Ubuntu 16.04网速慢解决方法
  19. 扫地机自动回充揭秘之石头T7 Pro
  20. 如何提升对编程的兴趣,在编程中找到快乐?

热门文章

  1. java实现批量下载图片并打包成zip
  2. 弧齿锥齿轮零件图_弧齿锥齿轮加工原理
  3. 解决方法:ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directo
  4. zabbix搭建详细全过程,以及遇到的问题
  5. 浪涌测试如何进行试验配置
  6. 解决SupportAssist提示意外错误的问题
  7. 云计算赛项--2020年赛题基础部分[任务3]
  8. meego linux版本,记MeeGo的多系统启动
  9. pta7-2 公路村村通
  10. [转载]python库收集贴