wordpress免插件自动添加meta信息

作者:matrix 发布时间:2014 年 1 月 30 日 分类:Wordpress

wordpress免插件自动添加meta信息

 

汗啊,今天都除夕了。完全没感觉~

WordPress主题没弄好meta信息或者根本没有那是经常的,这代码目测很实用的。

代码:

/* 
自动输出head的keywords和description信息 
*/  
/*截取字符*/  
function hhtjim_Substr($str, $len = 100){//默认的100  
    if(!$str){  
        return;  
    }  
    if( strlen( $str ) <= $len ){  
        return $str;  
    }else{  
        $ellipsis = '...';  
    }  
    $new_str = array();  
    for($i=0;$i<$len;$i++){  
        $temp_str=substr($str,0,1);  
        if(ord($temp_str) > 127){  
            $i++;  
            if($i<$len){  
                $new_str[]=substr($str,0,3);  
                $str=substr($str,3);  
            }  
        }else{  
            $new_str[]=substr($str,0,1);  
            $str=substr($str,1);  
        }  
    }  
    $new_str = join($new_str);  
    $new_str .=$ellipsis;  
    return $new_str;  
}  
/*去掉各类标签*/  
function hhtjim_Striptags($str,$allow = ''){  
    $str = str_replace(" ","",$str);//去掉空格  
    $str = str_replace('"','',$str);//去掉引号 
    $str = preg_replace('/(\r\n)|(\n)/', '', $str); // 消灭换行符 
    $str = preg_replace('/(\t)/', '', $str); // 消灭制表符 
    $str = strip_tags($str,$allow); //去掉html标签 
    $str = preg_replace('/\[(.+?)\]/', '', $str); // 消灭'[]'这样的标签 
    return $str; 
} 
function HHTjim_Keywords_Description(){ 
    global $post, $wp_query; 
    // 默认值 
$ds = get_option('description_announce')!=="" ? get_option('description_announce') :'HHTjim在互联网的个人博客。其中有分享&记录,更有不用解释的东西 -_-!  尽情欣赏吧  ^ _ ^'; 
$kw = get_option('key_announce')!=="" ? get_option('key_announce') : 'HHTjim,HHTjim.Com,部落格,个人博客,沫若中学'; 
    if(is_singular()){ // 普通页面 
        $keywords = array($keywords); 
        $keywords[] = get_post_meta($post->ID, 'Keywords', true); 
        $keywords[] = get_post_meta($post->ID, 'keywords', true); 
        // 仅对 单篇文章页( single ) 处理 
        if( is_single() ){ 
            //获得分类名称 作为关键字 
            $cats = get_the_category(); 
            if($cats){ 
                foreach( $cats as $cat ){ 
                    $keywords[] = $cat->name; 
                } 
            } 
            //获取Tags 将Tags 作为关键字 
            $tags = get_the_tags(); 
            if($tags){ 
                foreach( $tags as $tag ){ 
                    $keywords[] = $tag->name; 
                } 
            } 
        } 
        // 格式化处理 $keywords 
        if(count($keywords) > 1){ 
            array_shift($keywords); 
        } 
        $keywords = array_filter($keywords); 
        $keywords = join(',', $keywords); 
        // 对 description 的处理 
        if(!empty($post->post_password)){ // 受保护的文章 
            $keywords = ''; 
            $description = '请输入密码查看受保护的文章'; 
        }else{ 
            //获取自定义域内容 
             $description = mb_strimwidth(hhtjim_Striptags($post->post_content),0,117).'...'; 
        //  $description = hhtjim_Striptags($post->post_content); 
        //   $description = hhtjim_Substr($description); 
             if( empty($description) ){ 
                 $description = get_post_meta($post->ID, 'description', true); 
             } 
            //自定义域为空 试试Excerpt 
            if( empty($description) ){ 
                $description = get_the_excerpt(); 
            } 
            //依然为空 则截取文章的前220个字符作为描述 
            if( empty($description) ){ 
                $description = hhtjim_Striptags($post->post_content); 
                $description = hhtjim_Substr($description, 220); 
            } 
        } 
    }elseif(is_category()){ // 分类页 
        $keywords = single_cat_title('', false); 
        $description = hhtjim_Striptags(category_description()); 
    }elseif(is_author()){ // 作者页 
        $meta_auth = get_userdata(get_query_var('author')); 
        $keywords = $meta_auth->display_name; 
        $description = str_replace(array('"'), '"', $meta_auth->description);  
        $description = hhtjim_Striptags($description);  
    }elseif(is_tag()){ // 标签页  
        $keywords = single_cat_title('', false);  
        $description = tag_description();  
        $description = hhtjim_Striptags($description);  
    }elseif(is_month()){ // 月份存档页  
        $description = single_month_title(' ', false);  
    }  
    if( !emptyempty($keywords) ){  
        echo '<meta name="keywords" content="',trim($keywords),'" />',"\n";  
    }else{echo '<meta name="keywords" content="',trim($kw),'" />',"\n";}  
    if( !emptyempty($description) ){  
    if($description == '...'){  
        echo '<meta name="description" content="',trim($ds),'" />',"\n";  
    }else{  
        echo '<meta name="description" content="',trim($description),'" />',"\n";}  
    }else{echo '<meta name="description" content="',trim($ds),'" />',"\n";}  
    unset($keywords,$description);  
}  
add_action('wp_head', 'HHTjim_Keywords_Description',1);

说明:

代码放到WordPress主题的?>前面。

80行的117为普通文章页面的截取字数。

此代码扒自PhilNa2主题,超级强大。自己稍微修改,完善了些。