一、前言
有一些网站主经常查看自己网站是否被收录,今天枫少看到了一个不错的方案,是从张戈博客看到的,主要就是给文章标题下面添加一个收录情况的显示。
二、效果预览
三、纯代码部署
①、编辑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
|
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>’;
}
}
|
②、编辑WordPress主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:
1
|
<?php baidu_record(); ?>
|