欢迎来到合肥浪讯网络科技有限公司官网
  咨询服务热线:400-0678-380

非插件完成高效的SEO优化

发布时间:2019-01-09 文章来源:本站  浏览次数:72

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

搜索引擎优化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. }  

上一条:网站安全狗和360网站安...

下一条:给SEO新人一些有用的忠...