Drupal API

drupal7 如何在头部添加或删除一些meta ,html_head_alter

日期:2015-03-13 阅读:5201

dmandwp系统 - wordpress系统和DM系统区块建站>>

打开 html.tpl,你会发现  <?php print $head; ?>
如何改写这个 $head呢?

----------------

首先,drupal_set_html_head 只在drupal6的,

在drupal7,只能用  drupal_add_html_head
https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_html_head/7
写法:function yourtheme_preprocess_html(&$variables) {  //注意,必须是在preprocess_html下。不是preprocess_page
// First, we must set up an array
$element = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
  );
drupal_add_html_head($element, 'chrome_frame');
}


-------------

如果使用了metatag模块,那会产生一些keywords or description,那用drupal_add_html_head就会重复增加了,这时要用hook:
当然你也用hook_html_head_alter可以覆写它,在
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_html_head_alter/7
function yourtheme_html_head_alter(&$head_elements) {
  // Force the latest IE rendering engine and Google Chrome Frame.
  $head_elements['chrome_frame'] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array('http-equiv' => 'X-UA-Compatible', 'content' => 'IE=edge,chrome=1'),
  );
}
---------------
输出形式在:theme_html_tag 
https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_html_tag/7
覆写方式:<?php
function THEMENAME_preprocess_html_tag(&$variables) {
。。。
}

 

删除一些meta:
http://drupal.stackexchange.com/questions/588/remove-certain-meta-tags-in-drupal-7
How can I remove these tags from my pages in Drupal 7?

<link rel="shortlink" href=" .... " />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<link rel="canonical" href="...." />
Update: I have the code for template.php, which removes the generator and canonical tags.

function program_html_head_alter(&$head_elements) {
  unset($head_elements['system_meta_generator']);
  foreach ($head_elements as $key => $element) {
    if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
      unset($head_elements[$key]); 
    }
  }
}

---------------
这样,可以添加一些js或css,或seo: keywords or description.
seo可以用模块metatag

<<点击返回

Drupal7视频教程 Drupal7主题 Drupal7最新文档 Drupal7推荐文档
  • 暂无内容!
  • 暂无内容!