Today Sven will introduce some tips about wordpress Seo,SEO means Search Engine Optimization.If you wanna your wordpress site more attractive to search engine,first you would know search engine’s taste.There are lots of plugins which have the same function,but if you want to do it by yourself,follow Sven and do the following job:
First is Seo for header,add following code to header.php
Attention:if there are already title/description/keywords in header.php,first delete the old one.Never forget use your own information instead of the following one.^ ^
it can add tags als the keywords of the single page,it will help a lot.
<?phpif(is_home()) { ?><title>Sven's Blog,Sven的博客</title><meta content="Welcome to Sven's Blog!欢迎来到Sven的博客" name="Description"/><meta content="Sven,Sven Qin,Wordpress,Sven's Blog,sven的博客" name="Keywords"/><?php } ?><?phpif(is_category()) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title><meta content="<?php echo trim(strip_tags(category_description())); ?>" name="Description"/><meta content="<?php echo single_cat_title(); ?>" name="Keywords"/><?php } ?><?phpif(is_single()) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title><meta name="description" content="<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"......","utf-8"); ?>" /><?php$keywords = get_the_tags();$keyword = '';foreach ($keywords as $value) {$keyword .= $value->name.',';}?><meta name="keywords" content="<?php echo $keyword ;?>" /><?php }?><?phpif(is_tag()) { ?><title><?php echo trim(wp_title('',0)); ?>_tag page<? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?></title><meta name="description" content="<?php echo trim(strip_tags(tag_description())); ?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('page %s ',$paged); ?>" /><meta name="keywords" content="<?php echo trim(wp_title('',0)); ?>" /><?php }?>
Second code for header.php,it can use the first 100 byte as the description of the page.
<?phpif(is_single()) { ?><meta name="description" content="<?php echo trim(wp_title('',0)); ?>-<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100,"......","utf-8"); ?>" /><?php }?>
following code can add rel=”nofollow” to external links,just copy the code to functions.php:
add_filter( 'the_content', 'sven_seo_wl');function sven_seo_wl( $content ) { $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>"; if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { if( !empty($matches) ) { $srcUrl = get_option('siteurl'); for ($i=0; $i < count($matches); $i++) { $tag = $matches[$i][0]; $tag2 = $matches[$i][0]; $url = $matches[$i][0]; $noFollow = ''; $pattern = '/target\s*=\s*"\s*_blank\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>'); $tag .= $noFollow.'>'; $content = str_replace($tag2,$tag,$content); } } } } $content = str_replace(']]>', ']]>', $content); return $content;}
the same,adding the code to functions.php:
add_filter('the_content', 'svenseo');function svenseo($content) { global $post; $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i"; $replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>'; $content = preg_replace($pattern, $replacement, $content); return $content;}
That’s all.
联系客服