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(); ?>

比如将以上函数添加到了文章模板的副标题位置

其实放在文章页不太方便我们管理员查询和提交,所以建议直接放在文章分类列表页。

如以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>
© 版权声明
评论 抢沙发

请登录后发表评论

    暂无评论内容