多个og:Facebook的图像

时间:2012-11-01 作者:Danny Michel

事实上,我在这个网站上搜索这个问题的答案已经有一段时间了。正在寻找一种为facebook“回显/打印”多个og:Image的方法。我在这里只使用了“the\\u post\\u缩略图”

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 个回复
最合适的回答,由SO网友:Danny Michel 整理而成

通过以下方式解决

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\');

结束