How to use drupal_add_html_head() to add the same metatag multiple times?
-
10-12-2020 - |
Pergunta
I have a node which has multiple images attached to it. And each image should also be printed in a <meta property="og:image" content="image_url" />
metatag. I have tried to use drupal_add_html_head()
for each image in a template_preprocess_page()
, but only the last image is printed as metatag.
function mytheme_preprocess_page(&$variables) {
...
// add multiple og:image tags
foreach ($images as $image) {
$element = array(
'#tag' => 'meta',
'#attributes' => array(
"property" => "og:image",
"content" => $image,
),
);
drupal_add_html_head($element,'facebook_share_image');
}
}
Solução
The tags are stored by key, so using a different key for each should do the trick:
$delta = 0;
foreach ($images as $image) {
$element = array(
'#tag' => 'meta',
'#attributes' => array(
"property" => "og:image",
"content" => $image,
),
);
drupal_add_html_head($element,'facebook_share_image:' . $delta++);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange