WordPress主題給文章增加百度是否已收錄的功能,這個(gè)功能添加起來簡單,首先編輯主題目錄的functions.php文件,在最后一個(gè)?>前新增如下代碼并保存:
/**
* WordPress 顯示百度是否收錄功能(自定義欄目優(yōu)化版)
**/
function baidu_check($url,$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,'沒有找到該URL。您可以直接訪問') && !strpos($rs,'很抱歉,沒有找到與') ){
update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
function baidu_record() {
global $wpdb;
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
if(baidu_check(get_permalink($post_id), $post_id ) == 1) {
echo '<a target="_blank" title="點(diǎn)擊查看" rel="external nofollow" href="http://www.baidu.com/s?wd='.get_the_title().'">百度已收錄</a>';
} else {
echo '<a style="color:red;" rel="external nofollow" title="點(diǎn)擊提交,謝謝您!" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename='.get_permalink().'">百度未收錄</a>';
}
}第二步,編輯文章模板(一般是single.php,begin5.2是/template/content.php),在合適的位置添加如下代碼并保存:如果是begin主題的話可以參考begin5.2文件目錄詳情
<?php baidu_record(); ?>
若以上步驟都未出錯(cuò),那現(xiàn)在訪問文章頁面就能看到百度是否已收錄的效果了。你也可以把這段代碼放在首頁或喜歡的地方。
其實(shí)這個(gè)方法大家可以改造一下,加入到自己的網(wǎng)站系統(tǒng)中,都是一樣的。






