Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multiple Facebook OG:image tags for images attached to post WordPress

  • SOLVED

Hey there
Here's what I'm trying to do - for the purposes of Facebook, I need a code snippet which will take the urls of any images attached to a post, and output them in the <head> of the page, as og:image meta tags for Facebook.

There's lots online about using the featured image etc for this purpose, but i specifically need to grab all attached images for a post and provide them as options for facebook posting. Facebook accepts multiple og:image tags to provide options for the thumbnail when posting on Facebook.
The format of the FB meta tag is: <meta property="og:image" content="linktoimage.jpg"/>
So I need the code to create one of those for each attached image.

I have tried to use the below snippet but nothing is output into the <head>:
<?php function postimage($size=thumbnail, $qty=-1) {
if ( $images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => $qty,
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image( $image->ID, $size );

echo '<meta property="og:image" content="'.$attachmentimage.'"/>';
}
} else {
echo "No Image";
}
} ?>


Thanks!

=====

hey everyone - thank you for your responses to my question. i've tried all the various methods you have suggested below, but still not having any luck with this!!! can anyone see any issue with the methodology/logic being used in trying to extract the links to attached images?

Answers (4)

2011-10-25

Ryan Riatno answers:

Try to use hooks `add_action('wp_head', 'postimage');`
[[LINK href="http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head"]][[/LINK]]

2011-10-25

Jurre Hanema answers:

Actually <em>calling</em> your function postimage() would be the first thing to try...

Also, it's better to move the function postimage() to functions.php and call it through an action on wp_head as Ryan Riatno describes.

2011-10-25

Luis Cordova answers:

The reason why the function alone will not work is that it is completely unconnected to the other setup that makes it work. With hooks this is different. get_the_ID() or the like functions have no context, you need to ensure they do.

In functions.php insert this:


<?php
function postimage($size=thumbnail, $qty=-1) {
if ( $images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => $qty,
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image( $image->ID, $size );
echo '<meta property="og:image" content="'.$attachmentimage.'"/>';
}
} else {
echo "No Image";
}
}

add_action('wp_head', 'postimage');
?>


Luis Cordova comments:

you need to learn to debug, say what the error displays etc and go from there, else you don't get it, try always, and also check the other hints, and spread the prize :D


lucy b comments:

there is no php error! i only get the "no image" message returned.


Luis Cordova comments:

right no image means up the road the query did not return results or something
echo or var_export the variable and see what you get this time

2011-10-25

Luis Abarca answers:

Add single quotes to size argument and change 'numberposts' by 'posts_per_page'



function postimage($size = 'thumbnail', $qty = -1)
{
global $post;

$images = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'posts_per_page' => $qty,
'post_mime_type' => 'image')
);

if ( $images ) {
foreach( $images as $image ) {
$attachmenturl = wp_get_attachment_url($image->ID);
$attachmentimage = wp_get_attachment_image( $image->ID, $size );
echo '<meta property="og:image" content="'.$attachmentimage.'"/>';
}
} else {
echo "No Image";
}
}

add_action('wp_head', 'postimage');