给网站添加百度收录显示


avatar
szfx_blog 2021-08-31 1.24k

给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 style="color:green;font-size:12px;float: center" 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:center" 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>';
   }
}

前端文件编辑

找到文章页的模板如:single.php,在其特点位置添加代码

<?php baidu_record(); ?>

    显示IP属地
  • avatar
    游客

    不错。又学到一招。请问您的留言验证码是如何做的?我的网站没有验证码,经常收到垃圾留言

    • avatar
      博主

      @ ddasfsdf 我用的是Mimi Captcha验证码插件:https://cloud.szfx.top/wordpress/90.html

      • avatar
        游客

        @ szfx_blog 感谢答疑。博主技术很好哦,我见到你的好几个子域名,用了WordPress还有typecho. 厉害。

        • avatar
          博主

          @ ddasfsdf 谢谢支持!

发表评论

Captcha Code

相关阅读

隐藏
变装