自动设置节点的关键词和描述标签

.Wed, 12/15/2010 - 20:25

这个模块可以自动设置节点的关键词(keywords)和描述(description)标签。

1. 关键词取的是节点的所属分类,描述取的是body的前100个字符。

2. 还可以在 admin/settings/auto_meta_front  路径设置首页的关键词和描述标签。

SEO方面,对于究竟该含哪些标签才好,我了解的不是很透彻,总之大都说关键词和描述是很重要的,平常做项目用nodewords模块时也是只设置了这两种标签。这样一来,觉得nodewords有点大材小用了,就写了这个小模块,希望对朋友们有所帮助,因为代码很少,所以大家也可以集成到自己的模块代码中!

为了证明代码很少,这里就贴出全部代码...!(这高亮代码插件的缩进处理好像和我的习惯不太一样...  贴上去有些地方有点乱,包涵)

/**
 * @file
 * 自动设置节点的关键词和描述,可设置首页的关键词和描述。
 */

/**
 * Implements of hook_menu().
 */
function auto_meta_menu() {
  $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;
}

/**
 * Form callback of page callback of menu - 'admin/settings/auto_meta_front'.
 */
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);
}

/**
 * 预处理page模板
 */
function auto_meta_preprocess_page(&$vars) {
	/******** 首页 ********/
  if(drupal_is_front_page()) {
    drupal_set_html_head('<meta name="keywords" content="'.variable_get('front_keywords', '').'" />');
		drupal_set_html_head('<meta name="description" content="'.variable_get('front_description', '').'" />');

    $vars['head'] = drupal_get_html_head();
  }
	
	/******** 节点页 ********/
	if($vars['node']) {
		$node = $vars['node'];
		//描述
		$description = drupal_substr(strip_tags($node->content['body']['#value']), 0, 100);
		drupal_set_html_head('<meta name="description" content="'.$description.'" />');

		//关键词
		$keywords = '';
		foreach($node->taxonomy as $value) {
			$keywords .= $value->name.',';
		}
		drupal_set_html_head('<meta name="keywords" content="'.$keywords.'" />');

		$vars['head'] = drupal_get_html_head();
	}
}

模块见附件。

AttachmentSize
auto_meta.rar1.13 KB