编辑主题目录的 functions.php 文件,在新增如下代码:
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 '百度已收录';
} else {
echo '百度未收录';
}
}
WordPress 主题下的文章模板(一般是 single.php),在想要显示收录结果的位置添加如下代码并保存: