wordpress网站全自动SEO优化的方法
作者:来源:wordpress发表于:2020-06-17 21:34:00
网站内部的SEO无非就是控制链接的nofollow属性,图片添加alt标签,页面添加关键词(Keywords)页面描述(Description)与页面标题(title)这些,有时候单个单个来优化的确很麻烦,而使用数据库批量操作对于新手又太过于复杂,那么有什么办法让wordpress网站自动的来进行SEO优化呢?下面来教大家如何让wordpress网站自动做好站内的SEO优化。
首先是网站的Keywords、Description与title的优化,打开网站的header.php文件(PS:有些主题可能不同,但通常情况下是这个文件)加入以下代码:
<?phpif(is_home()) { ?><title>首页标题</title><meta content="首页描述" name="Description"/><meta content="首页关键词" name="Keywords"/><?php } ?><?php if(is_category()) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 页 ',$paged); ?></title><meta content="<?php echo trim(strip_tags(category_description())); ?>" name="Description"/><meta content="<?php echo single_cat_title(); ?>" name="Keywords"/><?php } ?><?php if(is_single()) { ?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo(name);?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %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 }?><?php if(is_tag()) { ?><title><?php echo trim(wp_title('',0)); ?>_标签tag页<? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 页 ',$paged); ?></title><meta name="description" content="<?php echo trim(strip_tags(tag_description())); ?><? $paged = get_query_var('paged'); if ( $paged > 1 ) printf('第 %s 页 ',$paged); ?>" /><meta name="keywords" content="<?php echo trim(wp_title('',0)); ?>" /><?php }?>
加上以上代码后wordpress就会对当前页面自动添加Keywords、Description与title属性,接下来是为文章中的外链添加nofollow属性。
add_filter( 'the_content', 'zyku_seo_wl');function zyku_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;}
将以上代码加入到当前主题的functions.php文件即可实现,换主题的时候记得把这段代码加到新主题里噢,不然换主题后文章中的外部链接就会变成无nofollow属性的了。
最后是给图片添加alt标签:
add_filter('the_content', 'zykuseo');function zykuseo($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;}
以上代码也是加入到functions.php,好了这样一来就实现了wordpress网站的自动SEO优化了。
相关文章
-
无相关信息
最新推荐
- 宝塔面板FTP链接服务器发回了不可路由地址
- 阿里云ecs安装宝塔面板失败原因?以及安装步骤?
- 织梦手机端-识别跳转
- html如何在一个form表单中实现多个submit
- thinkphp无限分类原理和实现代码
- thinkphp6 请求(request)变量
- 帝国CMS自定义列表SQL调用方法
- 帝国CMS自定义列表的使用实例
- 帝国cms自定义列表限制某个栏目显示
- PHP中利用for循环判断一个数是不是回文数
- PHP中判断一个数是否为素数的方法
- PHP中break,continue,exit的使用与区别
- 测试PHP连接MYSQL成功与否的代码
- PHP编程求最大公约数与最小公倍数的方法示例
- PHP获取数组中单列值的方法
- PHP使用一个for循环将N*N的二维数组的所有值置1实现方法
- HTML5+css3:3D旋转木马效果相册
- HTML,CSS,font-family:中文字体和英文名称对照表
- HTML使用栅格布局实现六种筛子样式的代码详解
- 修改CSS样式实现网页变灰色/黑白代码的几个方法整理
最新更新
- 黄庭坚《水调歌头·游览》我欲穿花寻路 直入白云深处
- 黄庭坚《品令·茶词》恰如灯下 故人万里 归来对影
- 杜荀鹤《溪兴》山雨溪风卷钓丝 瓦瓯篷底独斟时
- 储光羲《张谷田舍》一径入寒竹 小桥穿野花
- 赵嘏《寒塘》晓发梳临水 寒塘坐见秋
- 蔡确《夏日登车盖亭》纸屏石枕竹方床 手倦抛书午梦长
- 李峤《书》河图八卦出 洛范九畴初
- 刘长卿《饯别王十一南游》长江一帆远 落日五湖春
- 韦庄《章台夜思》芳草已云暮 故人殊未来
- 卢纶《宿澄上人院》竹窗闻远水 月出似溪中
- 孟浩然《留别王侍御维》只应守寂寞 还掩故园扉
- 苏轼《南乡子·送述古》归路晚风清 一枕初寒梦不成
- 王维《木兰柴》秋山敛馀照 飞鸟逐前侣
- 王维《栾家濑》跳波自相溅 白鹭惊复下
- 苏轼《少年游·润州作代人寄远》对酒卷帘邀明月 风露透窗纱
- 苏轼《行香子·过七里濑》一叶舟轻 双桨鸿惊 水天清 影湛波平
- 王维《汉江临泛》江流天地外 山色有无中
- 柳宗元《溪居》晓耕翻露草 夜榜响溪石
- 杜牧《赠宣州元处士》蓬蒿三亩居 宽于一天下
- 项鸿祚《清平乐·池上纳凉》水天清话 院静人销夏