當前位置:首頁 >  站長 >  建站經(jīng)驗 >  正文

WordPress優(yōu)化:查詢百度收錄,自動同步微博帶圖片等

 2015-01-30 19:29  來源: 免費資源部落   我來投稿 撤稿糾錯

  域名預訂/競價,好“米”不錯過

WordPress的流行不是由Wordpress程序本身決定的,而是由廣大的Wordpress開發(fā)者和愛好者推出來的。現(xiàn)在建站選擇Wordpress,最方便的就是可以在網(wǎng)上找到非常多的優(yōu)秀的免費Wordpress主題,還有各種用途的Wordpress插件,幾乎想要的功能就能用插件來代替。

對于Wordpress的折騰戶來說,沒有強大的興趣作支撐是很難日復一日地研究和推出新的Wordpress優(yōu)化技巧和方案,尤其是在國內(nèi)作為一個Wordpress開發(fā)者有時可能感覺很孤獨和寂寞。本篇文章就是來自熱愛WP的張戈博主自己在折騰Wordpress過程中開發(fā)出來的WP實用技巧。

有的朋友可能想要在自己的Wordpress的每篇文章上展示百度是否收錄的情況。還有的為了提高網(wǎng)站人氣,我們有必要將Wordpress的讀者制作一個漂亮的展示墻,在評論中顯示訪客UA信息。對于“要求高”的朋友,可能還想要讓自己的Wordpress的每篇文章的ID是連續(xù)的。

如果你有以上幾個需求,那么可以直接將本文分享的有關Wordpress優(yōu)化代碼拿去用了。前一天部落分享了WordPress同步更新到新浪微博,這次我們帶來了張戈博主改進優(yōu)化版本:讓WordPress發(fā)布文章自動同步到新浪微博,并帶上特色圖片。

WordPress優(yōu)化:查詢百度收錄,自動同步微博帶圖片,年月日排名的讀者墻一、查詢并展示W(wǎng)ordpress文章是否被收錄1、可能有的人會比較喜歡查詢自己博客上的每篇文章是否被百度收錄,并將結(jié)果展示出來,類似的效果如下:

2、實現(xiàn)在上述效果可以直接使用WP-Baidu-Record插件,官網(wǎng):

3、插件激活后,點擊[設置]按鈕進入插件設置界面,插件默認設置如下(默認是在文章內(nèi)容的最后輸出百度收錄結(jié)果,而且是公開顯示):

4、如果你想自定義輸出位置,那么請勾選自定義結(jié)果輸出位置,前往WordPress主題目錄,找到文章模板(一般是single.php),然后在想要顯示的位置添加如下函數(shù)即可:<?php echo baidu_record(); ?>

5、喜歡使用純代碼的話,編輯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=''.$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 target="_blank" title="點擊查看" rel="external nofollow" href="'.get_the_title().'">百度已收錄</a>';
   } else {
        echo '<a style="color:red;" rel="external nofollow" title="點擊提交,謝謝您!" target="_blank" href="'.get_permalink().'">百度未收錄</a>';
   }
}

6、然后編輯WordPress主題下的文章模板(一般是single.php),在想要顯示收錄結(jié)果的位置添加如下代碼并保存:<?php echo baidu_record(); ?>。和上文插件的安裝方法一致,可進一步參考操作。

7、代碼說明如下:

1、通過curl在百度查詢url收錄結(jié)果,如果已收錄就將結(jié)果寫入到文章的post meta記錄中。待下一次再次打開頁面時,先檢查文章自定義欄目字段來判斷是否已收錄,若已收錄則直接輸出,而不再執(zhí)行curl查詢。

2、若查詢結(jié)果為已收錄,亦會輸出一個在百度查詢文章標題的a標簽,用于查看文章排名,甚至可以查看是否被人轉(zhuǎn)載或篡改!

3、 管理員可以隨時在后臺文章編輯界面中的自定義欄目來修改是否已收錄的結(jié)果,自定義名稱為baidu_record,1為已收錄,0為未收錄。

二、 WordPress更新后自動同步文章到新浪微博1、Wordpress更新文章后自動將文章同步更新到新浪微博上,部落在WordPress同步更新到新浪微博有分享過,不過我們還想在自動同步更新文章時順便帶上文章的特色圖片,這樣讓我們的微博變得更有吸引力一些。

2、將之前的代碼修改下就可以了,代碼如下:

/*** WordPress發(fā)布文章同步到新浪微博(帶圖片&自定義欄目版)* 文章地址:*/function post_to_sina_weibo($post_ID) {   /* 此處修改為通過文章自定義欄目來判斷是否同步 */   if(get_post_meta($post_ID,'weibo_sync',true) == 1) return;   $get_post_info = get_post($post_ID);   $get_post_centent = get_post($post_ID)->post_content;   $get_post_title = get_post($post_ID)->post_title;   if ($get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish') {       $appkey='1034947262'; /* 此處是你的新浪微博appkey,不修改的話就會顯示來自張戈博客哦! */       $username='微博用戶名';       $userpassword='微博密碼';       $request = new WP_Http;       $keywords = "";        /* 獲取文章標簽關鍵詞 */       $tags = wp_get_post_tags($post_ID);       foreach ($tags as $tag ) {          $keywords = $keywords.'#'.$tag->name."#";       }      /* 修改了下風格,并添加文章關鍵詞作為微博話題,提高與其他相關微博的關聯(lián)率 */     $string1 = '【文章發(fā)布】' . strip_tags( $get_post_title ).':';     $string2 = $keywords.' 查看全文:'.get_permalink($post_ID);     /* 微博字數(shù)控制,避免超標同步失敗 */     $wb_num = (138 - WeiboLength($string1.$string2))*2;     $status = $string1.mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, $wb_num,'...').$string2;            /* 獲取特色圖片,如果沒設置就抓取文章第一張圖片 */        if (has_post_thumbnail()) {          $url = get_post_thumbnail_url($post->ID);            /* 抓取第一張圖片作為特色圖片,需要主題函數(shù)支持 */       } else if(function_exists('catch_first_image')) {          $url = catch_first_image();        }       /* 判斷是否存在圖片,定義不同的接口 */       if(!empty($url)){           $api_url = ''; /* 新的API接口地址 */           $body = array('status' => $status,'source' => $appkey,'url' => $url);       } else {           $api_url = '';           $body = array('status' => $status,'source' => $appkey);       }       $headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));       $result = $request->post($api_url, array('body' => $body,'headers' => $headers));       /* 若同步成功,則給新增自定義欄目weibo_sync,避免以后更新文章重復同步 */       add_post_meta($post_ID, 'weibo_sync', 1, true);    }}add_action('publish_post', 'post_to_sina_weibo', 0);/*//獲取微博字符長度函數(shù) */function WeiboLength($str){    $arr = arr_split_zh($str);   //先將字符串分割到數(shù)組中    foreach ($arr as $v){        $temp = ord($v);        //轉(zhuǎn)換為ASCII碼        if ($temp > 0 && $temp < 127) {            $len = $len+0.5;        }else{            $len ++;        }    }    return ceil($len);        //加一取整}/*//拆分字符串函數(shù),只支持 gb2312編碼  //參考:*/function arr_split_zh($tempaddtext){    $tempaddtext = iconv("UTF-8", "GBK//IGNORE", $tempaddtext);    $cind = 0;    $arr_cont=array();    for($i=0;$i<strlen($tempaddtext);$i++) style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px" if(strlen(substr($tempaddtext,$cind,1))="" {=""> 0){            if(ord(substr($tempaddtext,$cind,1)) < 0xA1 ){ //如果為英文則取1個字節(jié)                array_push($arr_cont,substr($tempaddtext,$cind,1));                $cind++;            }else{                array_push($arr_cont,substr($tempaddtext,$cind,2));                $cind+=2;            }        }    }    foreach ($arr_cont as &$row)    {        $row=iconv("gb2312","UTF-8",$row);    }    return $arr_cont;}

3、 如果你的主題沒有抓取文章第一張圖片作為特色圖片的功能,請額外添加如下代碼:


/* 抓取文章第一張圖片作為特色圖片(已加上是否已存在判斷,可放心添加到functions.php) */
if(!function_exists('catch_first_image')):
  function catch_first_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content,$matches);
    $first_img = $matches [1] [0];
    return $first_img;
   }

4、 一切準備就緒了,但是發(fā)布文章肯定不會同步,為啥?因為沒權限唄~!原來這個接口需要在原先的基礎上額外申請,點擊應用名稱,然后,在接口管理==>申請權限==>申請微博高級寫入權限:

5、有求于人,不管有多容易、門檻有多低,我們都要保持誠懇的態(tài)度:

6、一般情況,一個工作日之內(nèi)就能通過了,通過之后,你在去發(fā)布文章,就能看到效果了,不但有特色圖片,而且還顯示【來自XX博客】。

三、WordPress酷炫CSS3讀者墻:按年、月、日排名1、WordPress酷炫CSS3讀者墻這個玩意一般不用我多說,大部分用WordPress的博主都了解過了,為了增強評論的積極性,我們可以熱心讀者按月份排行,甚至還可以一周為單位,進一步激勵讀者評論留言。

2、實現(xiàn)以上效果,可以直接使用以下代碼(同時存在多個時間段):

<!-- 張戈博客修改版讀者墻排行代碼 -->
<style type="text/css">
.readers-list{line-height:18px;text-align:left;overflow:hidden;_zoom:1}
.readers-list li{width:200px;float:left;*margin-right:-1px}
.readers-list a,.readers-list a:hover strong{background-color:#f2f2f2;background-image:-webkit-linear-gradient(#f8f8f8,#f2f2f2);background-image:-moz-linear-gradient(#f8f8f8,#f2f2f2);background-image:linear-gradient(#f8f8f8,#f2f2f2)}
.readers-list a{position:relative;display:block;height:36px;margin:4px;padding:4px 4px 4px 44px;color:#999;overflow:hidden;border:#ccc 1px solid;border-radius:2px;box-shadow:#eee 0 0 2px}
.readers-list img,.readers-list em,.readers-list strong{-webkit-transition:all .2s ease-out;-moz-transition:all .2s ease-out;transition:all .2s ease-out}
.readers-list img{width:36px;height:36px;float:left;margin:0 8px 0 -40px;border-radius:2px}
.readers-list em{color:#666;font-style:normal;margin-right:10px}
.readers-list strong{color:#ddd;width:40px;text-align:right;position:absolute;right:6px;top:4px;font:bold 14px/16px microsoft yahei}
.readers-list a:hover{border-color:#bbb;box-shadow:#ccc 0 0 2px;background-color:#fff;background-image:none}
.readers-list a:hover img{opacity:.6;margin-left:0}
.readers-list a:hover em{color:#EE8B17;font:bold 12px/36px microsoft yahei}
.readers-list a:hover strong{color:#EE8B17;right:150px;top:0;text-align:center;border-right:#ccc 1px solid;height:44px;line-height:40px}
</style>
<h2>年度評論排行 TOP3</h2>
<?php  
    $query1="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date between date_sub(now(),interval 1 year) and now() AND user_id='0' AND comment_author_email != '改成你的郵箱賬號' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 3";  
    $wall = $wpdb->get_results($query1);   
    $maxNum = $wall[0]->cnt;  
    foreach ($wall as $comment)  
    {  
        $width = round(40 / ($maxNum / $comment->cnt),2);  
        //此處是對應的條的寬度  
        if( $comment->comment_author_url )   
          $url = $comment->comment_author_url;   
        else $url="#";
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );   
        $tmp = "<li><a rel=\"friend\" alt=\"avatar頭像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";  
        $output1 .= $tmp;  
     }   
    $output1 = "<ul class=\"readers-list\">".$output1."</ul>";   
    echo $output1 ;  
?>   
<!-- end 年度排行 --> 
<div class="clear"></div>           
<br />
<h2>本月評論排行 TOP6</h2>
<?php  
    $query2="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE date_format(comment_date,'%Y-%m')=date_format(now(),'%Y-%m') AND user_id='0' AND comment_author_email != '改成你的郵箱賬號' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6";  
    $wall = $wpdb->get_results($query2);   
    $maxNum = $wall[0]->cnt;  
    foreach ($wall as $comment)  
    {  
        $width = round(40 / ($maxNum / $comment->cnt),2);  
        //此處是對應的條的寬度  
        if( $comment->comment_author_url )   
          $url = $comment->comment_author_url;   
        else $url="#";
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );   
        $tmp = "<li><a rel=\"friend\" alt=\"avatar頭像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";  
        $output2 .= $tmp;  
     }   
    $output2 = "<ul class=\"readers-list\">".$output2."</ul>";   
    echo $output2 ;  
?>   
<!-- end 本月排行 -->
<div class="clear"></div>           
<br />
<h2>本周評論排行 TOP9</h2>
<?php  
    $query3="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now()) AND user_id='0' AND comment_author_email != '改成你的郵箱賬號' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 9";  
    $wall = $wpdb->get_results($query3);   
    $maxNum = $wall[0]->cnt;  
    foreach ($wall as $comment)  
    {  
        $width = round(40 / ($maxNum / $comment->cnt),2);  
        //此處是對應的條的寬度  
        if( $comment->comment_author_url )   
          $url = $comment->comment_author_url;   
        else $url="#";
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );   
        $tmp = "<li><a rel=\"friend\" alt=\"avatar頭像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";  
        $output3 .= $tmp;  
     }   
    $output3 = "<ul class=\"readers-list\">".$output3."</ul>";   
    echo $output3 ;  
?>   
<!-- end 本周排行·全部結(jié)束 -->

3、將以上代碼添加到主題目錄的留言板模塊的合適位置即可,代碼中的三種時間段,最關鍵性的地方就修改變量名,否則一樣的變量名會導致數(shù)據(jù)重復!需要修改的變量名是: output query,例如 output1 output2這種形式。

四、Wordpress優(yōu)化小結(jié)1、上面分享的幾個Wordpress優(yōu)化小技巧基本上都是大家平常折騰博客時要用到的,對于Wordpress有興趣的朋友也可以從這些代碼得到啟示,從而開發(fā)出自己的Wordpress實用功能來。

2、代碼和插件都是張戈博主原創(chuàng)的,代碼中都已經(jīng)有具體的說明,基本上都是可以直接拿來用在自己的Wordpress,當然你可能需要一定的Wordpress折騰基礎,例如添加函數(shù),修改JS和CSS文件等。

文章出自:張戈 博主投稿 由部落編輯整理,版權歸原作者所有。本站文章除注明出處外,皆為作者原創(chuàng)文章,可自由引用,但請注明來源。

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!

相關文章

  • 百度通過site查詢收錄網(wǎng)頁數(shù)量為何定格了

    相信有不少站長在做網(wǎng)站的時候,查詢網(wǎng)站收錄的時候,會發(fā)現(xiàn)一個問題,經(jīng)常查詢發(fā)現(xiàn)百度收錄頁面數(shù)量一致定格在某個數(shù)量上長期不動了,通過百度站長的后臺查詢發(fā)現(xiàn)又有收錄變化,這是為什么呢?小編今天簡單分析下關于百度收錄的知識。

  • 收錄量驟降的原因有哪些?這八點需要注意

    網(wǎng)站收錄一般不會無故下降,最大的可能就是文章質(zhì)量不佳,采集過多,再者就是被黑(服務器、網(wǎng)站程序、快照劫持等等),還有就是robots協(xié)議問題,網(wǎng)站外鏈與友鏈問題等等。說起來容易,但做起來可能就沒這么容易了

  • 如何查詢網(wǎng)頁被百度收錄的時間?

    對于現(xiàn)在的百度搜索引擎,可謂是越來越人性智能化,為了讓用戶搜索體驗的提高,提高了seo優(yōu)化的難度讓排名的難度加深,低質(zhì)量違規(guī)且不相關的網(wǎng)址擇被降權,唯有原創(chuàng)內(nèi)容好的網(wǎng)站排在前面,就提高了用戶百度搜索體驗的加深,所以百度做出了一項新的更新。最近,做seo這行大家都注意到百度更新了,就是網(wǎng)頁被收錄的時間

  • 百度收錄查詢 時時關注網(wǎng)站流量

    建站的艱辛不言而喻,有多少站長披星戴月,有時仍難逃被百度K的命運,這其中有太多的原因了,在此,筆者不一一列舉了。但有一點現(xiàn)象很奇特,大量的新網(wǎng)民和菜鳥新手喜歡用百度搜索,因為百度往往搜索到很多別處搜索不到的頁面,但內(nèi)容的匹配度則令人質(zhì)疑,而專業(yè)人

  • 簡介幾個與百度收錄查詢有關的工具

    做站的辛酸,相信各位站長都心有體會,我們不停地用著別人提供的東西筑造著自己的夢,成功有時,悲傷有時,但我們始終在延續(xù)著,這其間百味,筆者不想再多做陳述了。站長工具是我們做站時必不可少的助手,我們利用它們,時時關注網(wǎng)站發(fā)展的動態(tài),然而隨著時間

熱門排行

信息推薦