How do I add Facebook OpenGraph meta tags for attachments in the header for a custom theme?

Question

How do I add Facebook OpenGraph tags in the header when the header has already been called, and the values I need to add are obtained in the body? This is a highly customized theme (which I did not create).

Here is the image.php file which contains the attachment page for an image:

  <div class="posted-att">
    <?php 
      echo wp_get_attachment_image($attachment_id,'large');

I don’t know where $attachment_id is set. It’s not set in image.php. It’s set in several places in wp-admin and wp-includes but I’m not sure which ones are relevant.

I tried adding the following to header.php, but $attachment_id was null/0.

<?php wp_head(); ?>

<?php echo 'attachment id = ' . $attachment_id; ?>
<?php if ($attachment_id != null) : // Facebook OpenGraph
  $shareImage = wp_get_attachment_url($attachment_id); ?>
  <meta property="og:image" content="<?=$shareImage?>" />
<?php endif; ?>

I also found this http://www.wpbeginner.com/wp-themes/how-to-add-facebook-open-graph-meta-data-in-wordpress-themes/, but my posts don’t have featured images. I’m trying to share specific attached images.

0
, , , , Chloe 9 years 2014-09-09T22:25:49-05:00 0 Answers 100 views 0

Leave an answer

Browse
Browse