换Wordpress一段时间了,搜索引擎优化很重要!下面稍稍修改了下meta标签,使用方法见前文wordpress SEO meta
写完下面这段代码首先在我呸上试验了下,出现了个小问题…用微软的记事本保存为utf8后blog顶端多了一个换行。反复看了源文件没能解决问题,gg下发现utf8有两种保存格式:
a – utf8 就是微软记事本默认的utf8格式
b – utf8 no BOM 这个微软记事本没提供
以b模式保存修改后的文件上面的问题完全解决了!
<?php
function utf8_trim($str) {
$len = strlen($str);
$hex = '';
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ''.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
if ( is_single() ) {
$kw=get_post_meta($post->ID,'keywords',true);
$dn=$post->post_excerpt;
if($kw==''){
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$kw = $kw . $tag->name . ", ";
}
}
if($dn==''){
$dn=$description = utf8_trim(substr(strip_tags($post->post_content),0,220));
}?>
<meta name="keywords"content="<?php echo $kw;?>"/>
<meta name="description"content='<?php echo $dn;?>"/>
<?php }if ( is_home() ) { ?>
<meta name="keywords" content="<?php echo '我呸'; ?>" />
<meta name="description" content="<?php echo '技术为主、翻译为辅、稍带点闲言碎语'; ?>" />
<?php } ?>