如何删除或更改 Drupal 7 中的现有元标记?在 drupal 6 中有drupal_set_header
类似的东西,但 Drupal 7 对此一无所知。
有没有什么方法可以在没有额外模块的情况下做到这一点?目前我有 2 个元描述标签,我不想要那个。
您可以实现hook_html_head_alter()来更改 Drupal 7 中现有的 head 标签。
您还可以使用drupal_add_html_head()和drupal_add_html_head_link()函数代替旧的drupal_set_header()
.
如果您使用的是元标记模块,您可以实现hook_metatag_metatags_view_alter 。
function your-themme_html_head_alter(&$head_elements) {
$remove = array(
'apple-touch-icon57',
'apple-touch-icon72',
'apple-touch-icon114'
);
foreach ($remove as $key) {
if (isset($head_elements[$key])) {
unset($head_elements[$key]);
}
}
//add
$appleIcon57px = array('#tag' => 'link', '#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-57.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 163dpi)'),);
$appleIcon72px = array('#tag' => 'link','#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-72.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 132dpi)'),);
$appleIcon114px = array('#tag' => 'link','#type' => 'html_tag', '#attributes' => array('rel' => 'apple-touch-icon', 'href' => '/misc/AMU-NUMERIQUE-ICONE-114.png', 'type' => 'image/png', 'media' => 'screen and (resolution: 326dpi)'),);
$head_elements['apple-touch-icon57']=$appleIcon57px;
$head_elements['apple-touch-icon72']=$appleIcon72px;
$head_elements['apple-touch-icon114']=$appleIcon114px;
}
You can implement hook_menu() to add head tags in Drupal 7.
/**
* Implements hook_menu().
* @return array
*/
function module-name_menu() {
$items['add-metatags'] = array(
'page callback' => 'custom_metatags',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function custom_metatags() {
$html_head = array(
'description' => array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'Enter your meta description here.',
),
),
'keywords' => array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'keywords',
'content' => 'Enter your meta keywords here.',
),
),
);
foreach ($html_head as $key => $data) {
drupal_add_html_head($data, $key);
}
}
考虑安装https://www.drupal.org/project/metatag以通过 GUI 控制页面元标记。