1

I've actually been searching on this site for the answer to this question for quite some time. Looking for a way to 'echo/print' multiple og:images for facebook. What I have here used only 'the_post_thumbnail'

function fb_image() {
if (is_single()) {
    global $post;
    $feature_image = get_the_post_thumbnail($post->ID);
    $doc = new DOMDocument();
    $doc->loadHTML($feature_image);
    $imageTags = $doc->getElementsByTagName('img');

    foreach($imageTags as $tag) {
            $image_url = $tag->getAttribute('src');
    }
}
?>

<meta property="og:image" content="<?php echo $image_url; ?>" />
<?php }
add_action('wp_head', 'fb_image');
1
  • 1
    where are the images coming from? in what context do you want them added?
    – Milo
    Nov 1, 2012 at 18:29

1 Answer 1

2

Figured it out with

function postimage($size = 'thumbnail', $qty = -1) 
{
    if (is_single() && !is_home() && !wp_attachment_is_image()) {
    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="'.$attachmenturl.'"/>';
        }
    } else {
        echo "No Image";
    }
}
}    
add_action('wp_head', 'postimage');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.