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

admin2025-04-19  0

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 /, but my posts don't have featured images. I'm trying to share specific attached images.

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/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.

Share Improve this question asked Sep 10, 2014 at 3:25 ChloeChloe 2452 gold badges4 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

$attachment_id is simply the numerical ID assigned to any media you have uploaded. You can write:

echo wp_get_attachment_image('1','large');

to display the large size of image attachment with ID of 1.

You should setup a new query to get all of the attachment IDs associated with the current post, then you can

get_posts('post_type=attachment')

Check this example and modify the li's to become the meta property declaration instead: codex.wordpress/Function_Reference/wp_get_attachment_image#Display_all_images_as_a_list

You can get all images attached to the current post with something like this :

$images = get_posts(
        array(
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_parent'    => $post->ID, // the current post id
            'posts_per_page' => 1,
        )
    );

Your theme is most likely just using the first attachment or something similar.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745063097a282817.html

最新回复(0)