WordPressµÄÎÄÕÂ×Ô¶¯Ìí¼Ó¹Ø¼ü´Ê¼°¹Ø¼ü´ÊµÄSEOÓÅ»¯

5年以前  |  阅读数:683 次  |  编程语言:PHP 

ÍøÕ¾µÄ¹Ø¼u×Ö¼°ÍøÒ³ÃeÊo¹ØϵÍøÕ¾¶ÔËÑË÷ÒýÇaeµÄÓѺó̶ȣ¬Èç¹u×Ô¼ºÊÖ¶¯¼ÓÏÔȻ̫ÕÛÌÚÁË£¬ÄÇÈçºÎÈÃwordpress²(C)¿Í×Ô¶¯ÎªÃ¿ÆªÎÄÕÂ×Ô¶¯¹Ø¼u×Ö¼°ÍøÒ³ÃeÊo¡£Ã¿ÆªÎÄÕµÄÄÚÈݲ»Í¬£¬ÎÒÃǸÃÈçºÎÈÃwordpress×Ô¶¯Ìi¼ÓÎÄÕÂÃeÊoºÍ¹Ø¼u´ÊÄØ£¿ÏÂÃae¾ÍÈÃÎÒÃÇÀ´¿´¿´ÈçºÎ¸øwordpress×Ô¶¯Ìi¼ÓÎÄÕÂÃeÊoºÍ¹Ø¼u´Ê¡£
ÔÚÄaÖ÷ÌaµÄfunctions.phpÎļþÌi¼ÓÒÔÏ´uÂe£¬¸÷¸o´uÂeµÄ¹¦ÄܽaÎoÈçÏ£º


    add_action ( 'wp_head', 'wp_keywords' ); // Ìi¼Ó¹Ø¼u×Ö
    add_action ( 'wp_head', 'wp_description' ); // Ìi¼ÓÒ³ÃaeÃeÊo

    function wp_keywords() {
     global $s, $post;
     $keywords = '';
     if (is_single ()) { //Èç¹uÊÇÎÄÕÂÒ³£¬¹Ø¼u´ÊÔoÊÇ£º±eÇ(C)+*ÖÀaID
     if (get_the_tags ( $post->ID )) {
      foreach ( get_the_tags ( $post->ID ) as $tag )
      $keywords .= $tag->name . ', ';
     }
     foreach ( get_the_category ( $post->ID ) as $category )
      $keywords .= $category->cat_name . ', ';
     $keywords = substr_replace ( $keywords, '', - 2 );
     } elseif (is_home ()) {
     $keywords = 'ÎÒÊÇÖ÷Ò³¹Ø¼u´Ê'; //Ö÷Ò³¹Ø¼u´ÊÉeÖÃ
     } elseif (is_tag ()) { //±eÇ(C)Ò³¹Ø¼u´ÊÉeÖÃ
     $keywords = single_tag_title ( '', false );
     } elseif (is_category ()) {//*ÖÀaÒ³¹Ø¼u´ÊÉeÖÃ
     $keywords = single_cat_title ( '', false );
     } elseif (is_search ()) {//ËÑË÷Ò³¹Ø¼u´ÊÉeÖÃ
     $keywords = esc_html ( $s, 1 );
     } else {//ĬÈÏÒ³¹Ø¼u´ÊÉeÖÃ
     $keywords = trim ( wp_title ( '', false ) );
     }
     if ($keywords) { //Êa³o¹Ø¼u´Ê
     echo "<meta name=\"keywords\" content=\"$keywords\" />\n";
     }
    }

    function wp_description() {
     global $s, $post;
     $description = '';
     $blog_name = get_bloginfo ( 'name' );
     if (is_singular ()) { //ÎÄÕÂÒ³Èç¹u´aeÔÚÃeÊo×ֶΣ¬ÔoÏÔʾÃeÊo£¬*ñÔo½ØÈ¡ÎÄÕÂÄÚÈÝ
     if (! empty ( $post->post_excerpt )) {
      $text = $post->post_excerpt;
     } else {
      $text = $post->post_content;
     }
     $description = trim ( str_replace ( array (
      "\r\n",
      "\r",
      "\n",
      "¡¡",
      " " 
     ), " ", str_replace ( "\"", "'", strip_tags ( $text ) ) ) );
     if (! ($description))
      $description = $blog_name . "-" . trim ( wp_title ( '', false ) );
     } elseif (is_home ()) {//Ê×Ò³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . "-" . get_bloginfo ( 'description' ) .'Ê×Ò³ÒªÏÔʾµÄÃeÊo'; // Ê×i"Òª×Ô¼º¼Ó
     } elseif (is_tag ()) {//±eÇ(C)Ò³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . "ÓÐ¹Ø '" . single_tag_title ( '', false ) . "' µÄÎÄÕÂ";
     } elseif (is_category ()) {//*ÖÀaÒ³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . "ÓÐ¹Ø '" . single_cat_title ( '', false ) . "' µÄÎÄÕÂ";
     } elseif (is_archive ()) {//ÎĵµÒ³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . "ÔÚ: '" . trim ( wp_title ( '', false ) ) . "' µÄÎÄÕÂ";
     } elseif (is_search ()) {//ËÑË÷Ò³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' µÄËÑË÷½Y¹u";
     } else {//ĬÈÏÆaËuÒ³ÏÔʾÃeÊoÉeÖÃ
     $description = $blog_name . "ÓÐ¹Ø '" . trim ( wp_title ( '', false ) ) . "' µÄÎÄÕÂ";
     }
     //Êa³oÃeÊo
     $description = mb_substr ( $description, 0, 220, 'utf-8' ) . '..';
     echo "<meta name=\"description\" content=\"$description\" />\n";
    }

Í»³o¹Ø¼u×ÖÔÚËÑÑ°½a¹u£º


    function wps_highlight_results($text){
    if(is_search()){
    $sr = get_query_var('s');
    $keys = explode(" ",$sr);
    $text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong>'.$sr.'</strong>', $text);
    }
    return $text;
    }
    add_filter('the_excerpt', 'wps_highlight_results');
    add_filter('the_title', 'wps_highlight_results');

ʹÓô˴uÂe¶ÎÍ»³oÏÔʾËÑË÷´ÊÔÚÄaµÄ²(C)¿ÍËÑË÷½a¹uthe_excerptºÍthe_title¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例