WordPress实现检查当前文章是否被百度收录的功能(无需插件直接代码调用)

正常情况下,我们希望检查当前的文章是否被百度收录,我们需要复制这篇文章的URL或者标题直接丢到百度搜索引擎查看。那有没有比较简单且直观的办法呢?这里,如果我们有用WordPress程序的话,可以用到下面的代码调用自动检测当前文章是否被百度收录,且可以添加到当前的页面中看到具体的显示情况。

WordPress实现检查当前文章是否被百度收录的功能(无需插件直接代码调用)

我们看看如何实现的。

//百度收录展示
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>';
   }
}

这里我们添加到当前主题的 Functions.php文件中。

<?php baidu_record(); ?>

然后在我们需要的single.php模板主题中,然后对应合适的位置添加上这个代码。我们就可以在前端看到这个文章是否被百度收录,如果收录会看到”百度已收录”的提示。

投票 post
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享