WordPress站点描述关键字自动读取
网站的优化取决你网站的收录量和索引量,所以网站的SEO优化是必不可少的,下面示例可根据判断页面来实现自动设置描述。
代码示例
<?php
if (is_home()){
$keywords = "两颗蛋,WordPress主题,WordPress插件,WordPress二次定制开发";
$description = "两颗蛋专注WordPress开发,WordPress插件汉化,更完美的WordPress优化";
}
elseif (is_single()){
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag){
$keywords = $keywords.$tag->name.",";
}
$keywords = rtrim($keywords, ', ');
if($post->post_excerpt){
$description = $post->post_excerpt;
}else{
$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200);
}
}
elseif (is_page('别名名称')){ //如果页面 别名,
$keywords = "";
$description = "";
}
elseif (is_category()){
$keywords = single_cat_title('', false);
$description = category_description();
}
elseif (is_tag()){
$keywords = single_tag_title('', false);
$description = tag_description();
}
$keywords = trim(strip_tags($keywords));
$description = trim(strip_tags($description));
?>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
使用方法
写入头部(header.php)或进行替换原有描述函数。
撰写评论