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