网上有很多Drupal SEO 的模块,但是我还都没用过,因为我也觉得这样的代码很简单,所以懒得安装了。
在“似水流云 ”的博客里,看到了一段代码,所以修改了下就拿来用了。
另外,因为我的文章有两个字段,一个是文章分类,一个是文章标签,而keywords只是文章标签,所以我在 auto_meta_preprocess_page函数做了许多改动,比如我的keywords标签是field_story_keywords,如果 你的关键词字段名跟我的不一样,你必须换成你自己定义的字段名。修改后的这个代码只适合Drupal7,而似水流云的只适合Drupal6。
因为有些分类页面也要修改下title什么的,所以我又在template.php里写了个seo_page函数,然后在mytheme_preprocess_html里调用就行了。
<?php
$items [ 'admin/settings/auto_meta_front' ] = array(
'title' => '设置首页的关键词和描述标签' ,
'access arguments' => array( 'administer site configuration' ),
'page callback' => 'drupal_get_form' ,
'page arguments' => array( 'auto_meta_front_form' ),
'type' => MENU_NORMAL_ITEM ,
'weight' => 32 ,
);
return $items ;
}
function auto_meta_front_form () {
$form [ 'front_keywords' ] = array(
'#title' => '站点关键词' ,
'#type' => 'textfield' ,
'#default_value' => variable_get ( 'front_keywords' , '' ),
);
$form [ 'front_description' ] = array(
'#title' => '站点描述' ,
'#type' => 'textarea' ,
'#default_value' => variable_get ( 'front_description' , '' ),
);
return system_settings_form ( $form );
} function auto_meta_preprocess_page (& $vars ) {
$description = '' ;
$keywords = '' ;
if( drupal_is_front_page ()) {
$description = variable_get ( 'front_description' , '' );
$keywords = variable_get ( 'front_keywords' , '' );
}
if(isset( $vars [ 'node' ])&&!empty( $vars [ 'node' ])) {
$node = $vars [ 'node' ];
$safe_summary = !empty( $node -> body [ 'und' ][ 0 ][ 'safe_summary' ]) ? $node -> body [ 'und' ][ 0 ][ 'safe_summary' ] : '' ;
$safe_value = !empty( $node -> body [ 'und' ][ 0 ][ 'safe_value' ]) ? $node -> body [ 'und' ][ 0 ][ 'safe_value' ] : '' ;
$description = empty( $safe_summary ) ? $safe_value : $safe_summary ;
$description = drupal_substr ( strip_tags ( $description ), 0 , 100 );
if(isset( $node -> field_story_keywords [ 'und' ])&&!empty( $node -> field_story_keywords [ 'und' ])){
foreach( $node -> field_story_keywords [ 'und' ] as $value ) {
if(isset( $value [ 'taxonomy_term' ])) $keywords .= $value [ 'taxonomy_term' ]-> name . ',' ;
}
}
}
$meta_description = array(
'#type' => 'html_tag' ,
'#tag' => 'meta' ,
'#attributes' => array(
'name' => 'description' ,
'content' => $description
)
);
$meta_keywords = array(
'#type' => 'html_tag' ,
'#tag' => 'meta' ,
'#attributes' => array(
'name' => 'keywords' ,
'content' => $keywords
)
);
drupal_add_html_head ( $meta_keywords , 'meta_keywords' );
drupal_add_html_head ( $meta_description , 'meta_description' );
}
?>
<?php
//template.php
function seo_page ( $link , & $variables ){
$seo_data = array();
$seo_data [ 0 ] = array( 'title' => 'Drupal安装指南,Drupal7专业开发教程-溯游' , 'keywords' => 'Drupal安装指南,Drupal7专业开发教程' , 'description' => '' );
$seo_data [ 1 ] = array( 'title' => 'Drupal常用模块,Drupal7模块介绍开发-溯游' , 'keywords' => 'Drupal常用模块,Drupal7模块介绍,Drupal模块开发' , 'description' => '' );
$seo_data [ 2 ] = array( 'title' => 'Drupal主题下载,Drupal7主题制作指南-溯游' , 'keywords' => 'Drupal主题下载,Drupal7主题制作指南' , 'description' => '' );
$seo_data [ 3 ] = array( 'title' => 'Drupal中文资讯,Drupal7汉化及互联网那些事儿-溯游' , 'keywords' => 'Drupal中文,Drupal7汉化' , 'description' => '' );
$seo_data [ 4 ] = array( 'title' => 'Drupal blogs-溯游' , 'keywords' => 'drupal blogs' , 'description' => '' );
$data = array();
if( $link == 'taxonomy/term/12' ){
$data = $seo_data [ 1 ];
}elseif( $link == 'taxonomy/term/10' ){
$data = $seo_data [ 2 ];
}elseif( $link == 'node/7' ){
$data = $seo_data [ 0 ];
}elseif( $link == 'node/11' ){
$data = $seo_data [ 3 ];
}elseif( $link == 'blog' ){
$data = $seo_data [ 4 ];
}
if(!empty( $data )){
$meta_description = array(
'#type' => 'html_tag' ,
'#tag' => 'meta' ,
'#attributes' => array(
'name' => 'description' ,
'content' => $data [ 'description' ]
)
);
$meta_keywords = array(
'#type' => 'html_tag' ,
'#tag' => 'meta' ,
'#attributes' => array(
'name' => 'keywords' ,
'content' => $data [ 'keywords' ]
)
);
if(isset( $variables [ 'head_title' ])){ $variables [ 'head_title' ] = $data [ 'title' ];}
drupal_add_html_head ( $meta_keywords , 'meta_keywords' );
drupal_add_html_head ( $meta_description , 'meta_description' );
}
}
?>
转载自:
http://suyou.info/node/30