WordPress、添加显示百度是否收录

浮夸小生。
2019-08-14 / 1 评论 / 374 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年12月19日,已超过1217天没有更新,若内容或图片失效,请留言反馈。

吐槽[咒骂]对于小生我这种,服务器太过于垃圾,非常反感插件,下面我们来使用代码一样可以完成插件的功能,看操作#(酷)

[alert class="danger"]1、编辑主题目录的functions.php文件,在最后一个?>标签前新添如下代码并保存(如没有?>标签,则直接加到最后面):[/alert]

//百度收录展示
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 style="color:green;font-size:12px;float: right;" target="_blank" title="点击查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'"><i class="fa fa-paw fa-lx"></i>百度已收录</a>';
   } else {
        echo '<a style="color:red;font-size:12px;float: right;" rel="external nofollow" title="点击提交,谢谢您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'"><i class="fa fa-paw fa-lx"></i>百度未收录</a>';
   }
}

[alert class="danger"]2、最后添加一个钩子,编辑文章模板(一般是single.php),在合适的位置添加如下代码并保存:[/alert]

<?php baidu_record(); ?>

#(酷)[转圈]刷新看看,现在绝对有效果了!!!

0

评论 (1)

取消
  1. 头像
    VPS234
    Windows 10 · Google Chrome

    很多博客都喜欢用这个功能[aru_1],原来是这么实现的

    回复