国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

不用插件就能解決wordpress的SEO,修改functions.php加入如下代碼

2024-09-07 00:51:25
字體:
來源:轉載
供稿:網友

不用插件就能解決wordpress的SEO,修改functions.php加入如下代碼即可.

很多人使用插件對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.     // 描述 
  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 (!emptyempty($seo_desc)) { 
  12.             $content = $seo_desc
  13.         } else if (!emptyempty($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.     // 關鍵字 
  26.     $keys = get_post_meta($post->ID, 'mm_seo_keywords', true); 
  27.     $cats = get_the_category(); 
  28.     $tags = get_the_tags(); 
  29.     if (emptyempty($keys)) { 
  30.         if (!emptyempty($cats)) foreach($cats as $cat$keys .= $cat->name . ', '
  31.         if (!emptyempty($tags)) foreach($tags as $tag$keys .= $tag->name . ', '
  32.         $keys .= $default_keywords
  33.     } 
  34.     $output .= "/t/t" . '<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 .=  "/t/t" . '<meta name="robots" content="noindex,follow">' . "/n"
  41.         } else { 
  42.             $output .=  "/t/t" . '<meta name="robots" content="index,follow">' . "/n"
  43.         } 
  44.     } else if (is_home() || is_singular()) { 
  45.         $output .=  "/t/t" . '<meta name="robots" content="index,follow">' . "/n"
  46.     } else { 
  47.         $output .= "/t/t" . '<meta name="robots" content="noindex,follow">' . "/n"
  48.     } 
  49.  
  50.     // 標題 
  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 (!emptyempty($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;//開源軟件:Vevb.com 
  70.     else                              $seo_title = $name . ' | ' . $description
  71.  
  72.     $output .= "/t/t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "/n"
  73.  
  74.     return $output

以上方法包含了標題、關鍵字、描述、robots設置,你在需要使用的頁面添加以上函數.

以上函數可以在:主頁、文章頁、標簽頁、分類頁、歸檔頁、搜索頁、404頁面使用,如果你需要其他頁面,可以修改以上代碼實現.

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定南县| 浦东新区| 莱州市| 永胜县| 师宗县| 互助| 定安县| 大邑县| 迭部县| 凤台县| 资中县| 凯里市| 永登县| 师宗县| 扶沟县| 罗定市| 英山县| 建德市| 古交市| 会理县| 张家港市| 新乐市| 北碚区| 新和县| 昭觉县| 运城市| 衡阳市| 友谊县| 新宁县| 津市市| 湘潭市| 图们市| 温宿县| 巴中市| 临高县| 德阳市| 洪雅县| 都匀市| 峨眉山市| 嘉禾县| 商河县|