正常情況下,我們希望檢查當前的文章是否被百度收錄,我們需要復(fù)制這篇文章的URL或者標題直接丟到百度搜索引擎查看。那有沒有比較簡單且直觀的辦法呢?這里,如果我們有用WordPress程序的話,可以用到下面的代碼調(diào)用自動檢測當前文章是否被百度收錄,且可以添加到當前的頁面中看到具體的顯示情況。
我們看看如何實現(xiàn)的。
//百度收錄展示 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" ><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" ><i class="fa fa-paw fa-lx"></i>百度未收錄</a>'; } }
這里我們添加到當前主題的 Functions.php文件中。
<?php baidu_record(); ?>
然后在我們需要的single.php模板主題中,然后對應(yīng)合適的位置添加上這個代碼。我們就可以在前端看到這個文章是否被百度收錄,如果收錄會看到"百度已收錄"的提示。
評論