效果如图:
1.编辑WordPress主题目录下的functions.php文件,在最后一个?>标签之前,添加如下代码并保存:
// 查询百度是否收录文章
function baidu_check($url){
global $wpdb;
$post_id = ( null === $post_id ) ? get_the_ID() : $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() {
if(baidu_check(get_permalink()) == 1) {
echo '<a style="background: #4e89ff;color: #fff !important;padding: 2px 8px;border-radius: 50px;" target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'"><i class="fa fa-paw"></i>百度已收录</a>';
} else {
echo '<a style="color:blue;" rel="external nofollow" title="点击提交,谢谢!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收录</a>';
}
}
2.然后编辑WordPress主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:

抱歉!隐藏内容,请输入密码后可见!
请打开微信扫描右边二维码回复关键字“840”获取密码,也可以微信直接搜索“超卓网络”关注微信公众号获取密码。
评论前必须登录!
立即登录 注册