无意中逛WordPress大佬的博客发现个比较实用的小工具,通过简短代码实现文章快速查询和显示是否收录的代码,然后就记录了以下并且署名大佬的名称和源地址。
找到当前主题目录下的functions.php文件加入以下代码:
/** * WordPress 显示百度是否收录功能(自定义栏目优化版) * https://zhang.ge/4617.html * DIY By 张戈博客 **/ 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 '百度未收录'; } }
然后在当前主题文章模板(content.php 一般情况是这个名字的文件)合适位置加入以下代码
<?php baidu_record(); ?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容