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

5年以前  |  阅读数:687 次  |  编程语言: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¡£

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8