2

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');
    }
}
7

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++);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.