我们经常看到有些网友的博客标题下面有一个小标签,"百度已收录"、"百度未收录"。这样的功能到底是如何实现的呢?如果说要有用确实也没有用,但是如果有些朋友个人博客需要加上这个功能的还是可以实现的。如果有需要用插件就简单用插件实现,这里用无插件实现。
第一、代码部分


function baidu_check($url){<br> global $wpdb;
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;<br> $baidu_record = get_post_meta($post_id,'baidu_record',true);<br> if( $baidu_record != 1){
$url='http://www.baidu.com/s?wd='.$url;
$curl=curl_init();<br> curl_setopt($curl,CURLOPT_URL,$url);<br> curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$rs=curl_exec($curl);
curl_close($curl);<br> if(!strpos($rs,'没有找到')){
if( $baidu_record == 0){<br> update_post_meta($post_id, 'baidu_record', 1);
} else {
add_post_meta($post_id, 'baidu_record', 1, true);<br> }<br> return 1;<br> } else {<br> if( $baidu_record == false){
add_post_meta($post_id, 'baidu_record', 0, true);
}
return 0;
}
} else {
return 1;
}
}
function baidu_record() {
if(baidu_check(get_permalink()) == 1) {
echo '百度已收录';
} else {
echo '百度未收录';
}
}

将代码添加到当前主题Functions.php文件中。
第二、调用显示

<?php baidu_record(); ?>

在需要显示的当前文章页面single.php标题下面就可以看到。


扫描二维码,在手机上阅读!