笑鹰技术博客

 找回密码
 立即注册
笑鹰技术博客 门户 wordpress技术 查看内容

WordPress 如何通过非插件方式实现SEO优化

2019-5-31 13:33| 发布者: jiatingxingfu| 查看: 268| 评论: 0

摘要: WordPress有很多SEO插件来帮助进行搜索引擎优化。如果你不想使用插件,下面这个高效的代码,将使你的博客对搜索引擎更加友好。 将下面代码粘贴到你的functions.php文件: functionbasic_wp_seo(){ global$page,$p ...



WordPress有很多SEO插件来帮助进行搜索引擎优化。如果你不想使用插件,下面这个高效的代码,将使你的博客对搜索引擎更加友好。

WordPress 如何通过非插件方式实现SEO优化 三联

将下面代码粘贴到你的functions.php文件:

  1. function basic_wp_seo() {  
  2.     global $page$paged$post;  
  3.     $default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize  
  4.     $output = '';  
  5.   
  6.     // description  
  7.     $seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);  
  8.     $description = get_bloginfo('description', 'display');  
  9.     $pagedata = get_post($post->ID);  
  10.     if (is_singular()) {  
  11.         if (!empty($seo_desc)) {  
  12.             $content = $seo_desc;  
  13.         } else if (!empty($pagedata)) {  
  14.             $content = apply_filters('the_excerpt_rss', $pagedata->post_content);  
  15.             $content = substr(trim(strip_tags($content)), 0, 155);  
  16.             $content = preg_replace('#n#', ' ', $content);  
  17.             $content = preg_replace('#s{2,}#', ' ', $content);  
  18.             $content = trim($content);  
  19.         }   
  20.     } else {  
  21.         $content = $description;      
  22.     }  
  23.     $output .= '<meta name="description" content="' . esc_attr($content) . '">' . "n";  
  24.   
  25.     // keywords  
  26.     $keys = get_post_meta($post->ID, 'mm_seo_keywords', true);  
  27.     $cats = get_the_category();  
  28.     $tags = get_the_tags();  
  29.     if (empty($keys)) {  
  30.         if (!empty($cats)) foreach($cats as $cat$keys .= $cat->name . ', ';  
  31.         if (!empty($tags)) foreach($tags as $tag$keys .= $tag->name . ', ';  
  32.         $keys .= $default_keywords;  
  33.     }  
  34.     $output .= "tt" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "n";  
  35.   
  36.     // robots  
  37.     if (is_category() || is_tag()) {  
  38.         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
  39.         if ($paged > 1) {  
  40.             $output .=  "tt" . '<meta name="robots" content="noindex,follow">' . "n";  
  41.         } else {  
  42.             $output .=  "tt" . '<meta name="robots" content="index,follow">' . "n";  
  43.         }  
  44.     } else if (is_home() || is_singular()) {  
  45.         $output .=  "tt" . '<meta name="robots" content="index,follow">' . "n";  
  46.     } else {  
  47.         $output .= "tt" . '<meta name="robots" content="noindex,follow">' . "n";  
  48.     }  
  49.   
  50.     // title  
  51.     $title_custom = get_post_meta($post->ID, 'mm_seo_title', true);  
  52.     $url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');  
  53.     $name = get_bloginfo('name', 'display');  
  54.     $title = trim(wp_title('', false));  
  55.     $cat = single_cat_title('', false);  
  56.     $tag = single_tag_title('', false);  
  57.     $search = get_search_query();  
  58.   
  59.     if (!empty($title_custom)) $title = $title_custom;  
  60.     if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged$page));  
  61.     else $page_number = '';  
  62.   
  63.     if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;  
  64.     elseif (is_singular())            $seo_title = $title . ' | ' . $name;  
  65.     elseif (is_tag())                 $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;  
  66.     elseif (is_category())            $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;  
  67.     elseif (is_archive())             $seo_title = 'Archive: ' . $title . ' | ' . $name;  
  68.     elseif (is_search())              $seo_title = 'Search: ' . $search . ' | ' . $name;  
  69.     elseif (is_404())                 $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;  
  70.     else                              $seo_title = $name . ' | ' . $description;  
  71.   
  72.     $output .= "tt" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "n";  
  73.   
  74.     return $output;  
  75. }  

需修改一下$default_keywords 后面默认的关键字

使用方法:用下面代码:

  1. <?php echo basic_wp_seo(); ?>  

替换主题header.php模板

  1. <title></title>  

注:可能不同的主题有所区别。

文章来源:http://zmingcx.com/non-plug-in-for-efficient-seo-optimization.html


路过

雷人

握手

鲜花

鸡蛋

笑鹰技术博客 ( 闽ICP备17023750号-1 )

GMT+8, 2024-3-29 12:57 , Processed in 0.028601 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.4

Design By moke8

返回顶部