今天无言尝试着给自己的网站适配了由谷歌出品的AMP。
虽然百度也有MIP,但是据网上消息说百度MIP已经凉了,关键还在于无言不会弄啊~
而且WodPress已经有谷歌AMP插件了,直接安装就可以使用,完全省去了大部分纠结时间。
AMP插件下载
可以在WordPress仪表盘,安装插件处直接搜索 WordPress 的 AMP 插件安装。或者在 WordPress 插件库下载上传
[sgbtn_blue href=’https://wordpress.org/plugins/amp/’]AMP下载[/sgbtn_blue]
AMP插件使用
默认只需要文章,你可以根据自己需求勾选文章、页面、和媒体。
将AMP页面提交到百度
在你的 functions.php 文件中加入以下代码
PS:代码中的****请前往百度搜索资源平台 >> 网站支持 >> 数据引入 >> MIP& >> AMP 下方的数据提交中替换。
[sgbtn_red href=’https://ziyuan.baidu.com/mip/index’]AMP 推送接口调用地址[/sgbtn_red]
/** * WordPress增加谷歌AMP加速移动页面并自动推送到百度 * 代码来源:https://www.dujin.org/12609.html */ if(!function_exists('Baidu_amp')){ function Baidu_amp($post_ID) { //已成功推送的文章不再推送 if(get_post_meta($post_ID,'Baiduamp',true) == 1) return; $url = get_permalink($post_ID); if(get_post_type($post_ID)=='page'){ $url=$url.'?amp'; } if(get_post_type($post_ID)=='post'){ $url=$url.'/amp/'; } $api = 'http://data.zz.baidu.com/urls?site=***************&token=*************=amp'; //登录百度搜索资源平台 >> 网站支持 >> 数据引入 >> MIP& >> AMP 下方的数据提交就能看到 AMP 推送接口调用地址 $request = new WP_Http; $result = $request->request( $api , array( 'method' => 'POST', 'body' => $url , 'headers' => 'Content-Type: text/plain') ); $result = json_decode($result['body'],true); //如果推送成功则在文章新增自定义栏目 Baiduamp,值为 1 if (array_key_exists('success_amp',$result)) { add_post_meta($post_ID, 'Baiduamp', 1, true); } } add_action('publish_post', 'Baidu_amp', 0); }
谢谢楼
谢谢楼主分享