上一次我们分享了一款实现百度收录的查询和显示的WordPress插件,具体请看《百度收录查询和显示的WordPress插件wp-baidu-record》。理论上,任何WordPress插件都能采用纯代码来替代,所以今天boke112导航继续分享一下纯代码部署的方法。
温馨提示:代码版和插件版不能同时存在,否则会产生冲突!
WordPress实现百度收录的查询和显示的代码部署如下:
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,'没有找到')){
if( $baidu_record == 0){
update_post_meta($post_id, 'baidu_record', 1);
} else {
add_post_meta($post_id, 'baidu_record', 1, true);
}
return 1;
} else {
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 '<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>';
}
}
2、编辑WordPress主题下的文章模板(一般是single.php),在想要显示收录结果的位置添加如下代码并保存:
<?php echo baidu_record(); ?>
比如,张戈博客是将以上函数添加到了文章模板的副标题位置:
来源:原创插件:百度收录查询和显示WordPress插件(自定义栏目优化版)
其实boke112觉得放在文章页不太方便我们管理员查询和提交,所以建议直接放在文章分类列表页。
如以Three主题为例,我们打开archive.php文件,在显示标签的后面加上显示百度收录情况的代码,如
<span class="archive-tag"><?php the_tags('','',''); baidu_record();?> </span>
如果不想给其他人看到这个收录情况,可以加上一个判断语句,像本站就是只有管理员能看到这个,具体代码如下:
<span class="archive-tag"><?php the_tags('','',''); if ( is_user_logged_in()){baidu_record();}?> </span>