歡迎來到合肥浪訊網(wǎng)絡(luò)科技有限公司官網(wǎng)
  咨詢服務(wù)熱線:400-099-8848

非插件完成高效的SEO優(yōu)化

發(fā)布時(shí)間:2019-01-09 文章來源:本站  瀏覽次數(shù):3126

WordPress有許多SEO插件來幫助進(jìn)行搜索引擎優(yōu)化。如果你不想使用插件,下面這個(gè)高效的代碼,將使你的博客對搜索引擎更加友愛。

搜索引擎優(yōu)化1

將下面代碼張貼到你的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. // description
  6. $seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true);
  7. $description = get_bloginfo('description', 'display');
  8. $pagedata = get_post($post->ID);
  9. if (is_singular()) {
  10. if (!empty($seo_desc)) {
  11. $content = $seo_desc;
  12. } else if (!empty($pagedata)) {
  13. $content = apply_filters('the_excerpt_rss', $pagedata->post_content);
  14. $content = substr(trim(strip_tags($content)), 0, 155);
  15. $content = preg_replace('#\n#', ' ', $content);
  16. $content = preg_replace('#\s{2,}#', ' ', $content);
  17. $content = trim($content);
  18. }
  19. } else {
  20. $content = $description;
  21. }
  22. $output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n";
  23. // keywords
  24. $keys = get_post_meta($post->ID, 'mm_seo_keywords', true);
  25. $cats = get_the_category();
  26. $tags = get_the_tags();
  27. if (empty($keys)) {
  28. if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', ';
  29. if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', ';
  30. $keys .= $default_keywords;
  31. }
  32. $output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n";
  33. // robots
  34. if (is_category() || is_tag()) {
  35. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  36. if ($paged > 1) {
  37. $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
  38. } else {
  39. $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
  40. }
  41. } else if (is_home() || is_singular()) {
  42. $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n";
  43. } else {
  44. $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n";
  45. }
  46. // title
  47. $title_custom = get_post_meta($post->ID, 'mm_seo_title', true);
  48. $url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/');
  49. $name = get_bloginfo('name', 'display');
  50. $title = trim(wp_title('', false));
  51. $cat = single_cat_title('', false);
  52. $tag = single_tag_title('', false);
  53. $search = get_search_query();
  54. if (!empty($title_custom)) $title = $title_custom;
  55. if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page));
  56. else $page_number = '';
  57. if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description;
  58. elseif (is_singular()) $seo_title = $title . ' | ' . $name;
  59. elseif (is_tag()) $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name;
  60. elseif (is_category()) $seo_title = 'Category Archive: ' . $cat . ' | ' . $name;
  61. elseif (is_archive()) $seo_title = 'Archive: ' . $title . ' | ' . $name;
  62. elseif (is_search()) $seo_title = 'Search: ' . $search . ' | ' . $name;
  63. elseif (is_404()) $seo_title = '404 - Not Found: ' . $url . ' | ' . $name;
  64. else $seo_title = $name . ' | ' . $description;
  65. $output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n";
  66. return $output;
  67. }  

上一條:網(wǎng)站安全狗和360網(wǎng)站安...

下一條:給SEO新人一些有用的忠...