WordPress如何查看文章是否被百度收录

追格官方小助手/ 2022年07月05日/ WordPress/ 浏览 1638

如标题所述,WordPress如何查看当前文章是否被百度收录,追格小编一般都是通过连接或标题搜索,但这种效率比较低下,是否有其他解决办法,我们一起来看看。


在当前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: 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>';
   }
}


在当前WordPress模板 single.php 相应位置中添加以下代码即可。


<?php baidu_record(); ?>


若该文被百度收录,文章即会提示已收录。

发表评论

暂无评论,抢个沙发...

客服 工单