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

How do I add Facebook OpenGraph tags in the header when the header has already been called, and the values I need to add

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745063095a4609084.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信