如何设置 WordPress 和 AMP:加速移动页面:附加设置分析、AdSense、社交媒体指南
已发表: 2016-09-25
最近几天有很多关于 Google AMP 的讨论。 Google 正式宣布将 AMP 页面支持作为移动搜索页面的一部分。 这真的很有趣。 到目前为止,我出于某种原因避免在 Crunchify 上实施 AMP,并且从未觉得它有任何重要性。
但是在查看了 Barry 的推文后,我也决定AMPlify
Crunchify。
您在寻找 AMP 和 wordpress 提示吗? 您想使用 Google 的 Accelerated Mobile Pages 来增强您的 WordPress 网站吗? 或者您想在 WordPress 中安装 AMP? 在本教程中,我们将详细介绍如何通过所有详细步骤对您的 WordPress 网站进行 AMPlify。
什么是加速移动页面 (AMP)?
Accelerated Mobile Pages
(AMP) 项目是一个开源项目,在创建时牢记速度,它创建了一次移动优化内容,并在任何地方立即加载。 页面上根本没有加载 JavaScript。 具有严格限制的 CSS 加载非常少。 我注意到你甚至不能在 CSS 中使用!important
的观察结果之一。
有以下任何问题吗?
- Google AMP:它是什么以及它是否适合您的 WordPress 网站
- 为 AMP 设置 WordPress 的最佳方式
- 如何从 Google AMP 页面和 WordPress 开始
- 加速移动页面wordpress插件
- WordPress 放大器页面
- Yoast amp 胶水和 wordpress 查询
让我们开始吧:为您的 WordPress 帖子启用 AMP
第1步
安装官方 AMP 插件。

- 激活后,一切就绪。 您不会看到任何选项菜单。
- 您必须转到
Settings
->“Permalinks
”菜单。 单击Save Changes
而不进行任何更改。
官方 AMP 插件提供基本功能,您的所有帖子都将自动 AMPlify。 您可以通过在任何帖子 URL 的末尾添加/amp/
来访问您的帖子的 AMPlified 版本。
看一个例子:
-
Original
帖网址:https://crunchify.com/how-to-sort-hashmap-by-key-and-value-in-java8-complete-tutorial/ -
AMPlify
发布网址:https://crunchify.com/how-to-sort-hashmap-by-key-and-value-in-java8-complete-tutorial/amp/
第2步
为 Yoast SEO 和 AMP 插件安装 Glue。 我相信从0.4 AMP WordPress version
开始,您不需要任何其他与 AMP 相关的插件。
现在:
- 转到
Appearance -> AMP
。 - 提供
Header
和Background
颜色,然后单击Save
。

第三步
我看到这个插件的唯一问题是它在页面底部添加了Powered by WordPress
链接。 为了更改它,您可能需要使用以下代码更改footer.php
页面。
- 转到
Plugins
->Editor
- 选择插件
AMP
- 选择文件 amp/templates/
footer.php
1 2 3 4 5 6 7 8 9 |
< footer class = "amp-wp-footer" > < div > < h2 > <?php echo esc_html ( $this - > get ( 'blog_name' ) ) ; ?> < / h2 > < p > Copyright < i class = "fa fa-copyright" aria - hidden = "true" > < / i > 2012 - 16 < a href = "https://crunchify.com" > Crunchify , LLC . < / a > • < a href = "https://crunchify.com/sitemap/" target = "_blank" > Archives < / a > • < a href = "https://crunchify.com/advertise/" target = "_blank" > Advertise < / a > < / p > < a href = "#top" class = "back-to-top" > <?php _e ( 'Back to top' , 'amp' ) ; ?> < / a > < / div > < / footer > |
根据您的需要修改版权行代码

第四步
将Analytics
和Social Media
图标添加到您的 AMP 页面。
-
crunchify_amp_add_analytics_social_ads_js
函数会将所需的分析和社交媒体javascript
代码添加到您的 amp 页面。 -
crunchify_amp_add_analytics_social_code
函数将添加实际的分析代码和社交媒体图标。 - 请确保在下方更新您的
Google Analytics Universal tracking ID
和Facebook App ID
。
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 |
// ~~~~~~~~~~~~~~~~~~~~~ AMP Changes Start ~~~~~~~~~~~~~~~~~~~ add_action ( 'amp_post_template_head' , 'crunchify_amp_add_analytics_social_ads_js' ) ; function crunchify_amp_add_analytics_social_ads_js ( $ amp_template ) { $ post_id = $ amp_template - > get ( 'post_id' ) ; ? > <script async custom-element = "amp-analytics" src = "https://cdn.ampproject.org/v0/amp-analytics-0.1.js" > </script> <script async custom-element = "amp-social-share" src = "https://cdn.ampproject.org/v0/amp-social-share-0.1.js" > </script> <?php } add_action ( 'amp_post_template_footer' , 'crunchify_amp_add_analytics_social_code' ) ; function crunchify_amp_add_analytics_social_code ( $amp_template ) { $post_id = $amp_template - > get ( 'post_id' ) ; ?> < amp - analytics type = "googleanalytics" id = "analytics1" > <script type = "application/json" > { "vars" : { "account" : "UA-43876549-1" // Modify universal tracking ID } , "triggers" : { "trackPageview" : { "on" : "visible" , "request" : "pageview" } } } </script> < / amp - analytics > // Social Media Code < div align = "center" > < amp - social - share type = "twitter" width = "30" height = "30" > < / amp - social - share > < amp - social - share type = "gplus" width = "30" height = "30" > < / amp - social - share > < amp - social - share type = "facebook" width = "30" height = "30" data - param - app_id = "509582389234904" > < / amp - social - share > // Modify your App ID < / div > < ? php } // ~~~~~~~~~~~~~~~~~~~~~ AMP Changes End ~~~~~~~~~~~~~~~~~~~ |
第 5 步
将 Google Adsense 代码添加到您的 AMP 页面。 就我而言,我想在特色图片下方添加300x250
的 Adsense 代码。 没有任何官方挂钩可供使用,因此我修改了位于 <AMP Plugin directory> amp/templates/single.php
的single.php
文件。


- 转到
Plugins
菜单 - 单击
Editor
- 选择
AMP
插件 - 转到
single.php
文件 - 添加 Adsense 代码,如下面的代码片段所示。 只需
look for line featured-image
并在此之后添加 Adsense 代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $this - > load_parts ( array ( 'featured-image' ) ) ; ?> // Adsense code START < br > < div align = "center" > < amp - ad width = 300 height = 250 type = "adsense" data - ad - client = "ca-pub-xxxxxxxxxxxxxxxx" data - ad - slot = "0110036438" > < div placeholder > < / div > < div fallback > < / div > < / amp - ad > < / div > // Adsense code END < div class = "amp-wp-article-content" > |
你都准备好了。 只需清理您的网站缓存并查看实时 AMP 页面。 您将看到所有 Google Analytics、Adsense 和社交媒体图标正常工作
怎么办?
让我们验证并检查您的 AMP 页面是否存在任何问题。 转到 https://validator.ampproject.org/ 并检查结果。

Google 将如何抓取 AMP Post 页面?
现在您的页面同时拥有AMP and non-AMP versions
,AMP 插件会自动将以下关系元标记添加到您的两个页面。
在非 AMP 页面上检查
您应该看到该页面的 AMP 版本的参考,以便让 Google 和其他平台了解它:
<link rel="amphtml" href="https://crunchify.com/how-to-sort-hashmap-by-key-and-value-in-java8-complete-tutorial/amp/">

在 AMP 页面上检查
您应该会看到引用其非 AMP 规范版本的文本:
<link rel="canonical" href="https://crunchify.com/how-to-sort-hashmap-by-key-and-value-in-java8-complete-tutorial/">

现在让我们等待几天或至少一周,您应该会在 Google Search Console 中看到 AMPlify 页面。 对我来说,这只是 4 天,我看到近 500 个页面已经为 AMP 结果编入索引

如何查看实时结果?
只需在移动浏览器上的 Google 中搜索帖子,您应该会在搜索结果页面中看到AMP result
。

另外,请继续访问此页面或添加书签,因为我会根据我的学习不断更新本教程。 快乐的博客和帮助加快网络速度。