现在很多博客网站都会加入百度文章收录情况检测,chrafz将方法写出来供大家学习。

WordPress添加百度文章收录情况检测 张弦先生のchrafz.com
如图就是自动检测当前URL是否被百度收录,如果收录了就显示”百度已收录”,如果没有收录就显示为”百度未收录”,而且我们可以直接点击它到百度站长平台提交当前页面的URL,以方便蜘蛛发现新文章,从而加快文章的收录速度。
注:此项功能需要服务器管理员开启PHP的 cURL扩展,否则无效哦!
functions.php
打开Wordpress当前主题目录下的的functions.php文件,在最后一个?>标签前新添如下代码并保存(如没有?>标签,则直接加到最后面):
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 |
//WordPress添加百度文章收录情况检测 //chrafz.com function baidu_check($url, $post_id){ $baidu_record = get_post_meta($post_id,'baidu_record',true); if( $baidu_record != 1){ $url='http://www.baidu.com/s?wd='.$url; $curl=curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $rs=curl_exec($curl); curl_close($curl); if(!strpos($rs,'没有找到该URL。您可以直接访问') && !strpos($rs,'很抱歉,没有找到与') ){ update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true); return 1; } else { return 0; } } else { return 1; } } function baidu_record() { global $wpdb; $post_id = ( null === $post_id ) ? get_the_ID() : $post_id; if(baidu_check(get_permalink($post_id), $post_id ) == 1) { echo '<a target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'">百度已收录</a>'; } else { echo '<a style="color:red;" rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>'; } } |
到这里,你可就别以为完工了哦,我们只是在functions.php里面注册好了要实现的功能,在需要的地方调用就好。
我们添加一行代码至你要调用收录情况的位置,比如我就是把它放在了文章页显示文章评论数的后面.我们打开编辑主题目录下的single.php
将下面这行代码在需要的地方调用即可:
1 |
<?php baidu_record(); ?> |
返回网站,随便打开一篇文章,就可以看到收录情况了
评论1