【问题标题】:Add Meta tag to specific pages only仅将元标记添加到特定页面
【发布时间】:2012-10-01 22:31:20
【问题描述】:
我是 Drupal 7 的新手。
我有不同的基本页面,我想使用元标记遍历每个页面:
<meta http-equiv="refresh" content="20; url=http://sitename/node/page2" />
第 2 页会有元标记
<meta http-equiv="refresh" content="10; url=http://sitename/node/page3" />
我该怎么做?我希望仅在基本页面上添加元标记
我尝试使用 TEMPLATEUSED_preprocess_html 添加元标记,但我已经意识到这是错误的,因为它不是动态的并且适用于每个页面。
【问题讨论】:
标签:
drupal
drupal-7
meta-tags
【解决方案1】:
drupal_add_html_head 用于在头部添加标签,请查看下面的示例。
// First, we must set up an array
$element = array(
'#tag' => 'link', // The #tag is the html tag - <link />
'#attributes' => array( // Set up an array of attributes inside the tag
'href' => 'http://fonts.googleapis.com/css?family=Cardo&subset=latin',
'rel' => 'stylesheet',
'type' => 'text/css',
),
);
drupal_add_html_head($element, 'google_font_cardo');
这将输出以下 HTML:
<link href="http://fonts.googleapis.com/css?family=Cardo&subset=latin" rel="stylesheet" type="text/css" />
【解决方案2】:
这是一个老问题,但仍然没有更具体的回答。看来我们必须使用 preprocess_html 在 template.php 中添加一个 head 元素。 $node 不可用(至少我无法从 template.php/preprocess_html 中访问它)但我可以使用 drupal_get_path_alias 获取路径,这是原始问题所要求的。
这是我的工作示例:
function THEMENAME_preprocess_html(&$variables) {
if (drupal_get_path_alias() == "node/45") {
$meta_refresh = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'refresh',
'content' => '900, url=/node/45',
),
);
drupal_add_html_head($meta_refresh, 'meta_refresh');
}
}
使用 case 语句可能更好地服务于 Warren 的两条路径。