我们经常看到有些网友的博客标题下面有一个小标签,”百度已收录”、”百度未收录”。这样的功能到底是如何实现的呢?如果说要有用确实也没有用,但是如果有些朋友个人博客需要加上这个功能的还是可以实现的。如果有需要用插件就简单用插件实现,这里用无插件用简单函数来实现。
代码部分
function baidu_check($url){ $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,'没有找到')){ return 1; }else{ return 0; } } function baidu_record() { if(baidu_check(get_permalink()) == 1) { echo '<a title="点击查看" href="https://www.baidu.com/s?ie=UTF-8&wd='.get_the_title().'" target="_blank" rel="external nofollow noopener">百度已收录</a>'; } else { echo '<a style="color: red;" title="一键帮忙提交给百度,谢谢您!" href="https://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'" target="_blank" rel="external nofollow noopener">推荐给百度</a>'; } }
将代码添加到当前主题Functions.php文件中。
调用显示
<?php baidu_record(); ?>
在需要显示的当前文章页面single.php标题下调用即可。