WordPress - og:image 打开图表以获取作者头像

WordPress - og:image open graph to fetch the authors avatar

我是 WordPress 的新手,我在 Facebook 开放图表方面遇到了一个小问题。 搜索了 6 个多月,但我总是失败。

我的网站有多个作者,我需要为 author.php "author profile" 添加 og:image 元标记,我被卡住了,我真的厌倦了搜索。

当用户试图分享他的个人资料时..头像图像不出现!因为Facebook找不到作者头像og:image!

我使用的插件:Yoast SEO, Simple Local Avatars, AddToAny Share Buttons

终于找到答案了,想分享给大家,但是还有一个小问题。 现在我可以得到作者的头像,如果他至少有 1 post! 0 posts Facebook 的作者无法获取他的头像!!奇怪的事情,我不知道如何解决这个问题。

无论如何这是我找到的代码。

add_action('wpseo_head', 'add_fb_og', 5);

function add_fb_og() {
        if ( is_author ( ) ) {
$my_custom_avatar = get_avatar(get_the_author_meta( 'ID' ), 200 );
$document = new DOMDocument();
@$document->loadHTML($my_custom_avatar);
$nodes = $document->getElementsByTagName('img');

    ?>
    <meta property="og:image" content="<?php echo $nodes->item(0)->getAttribute('src'); ?>" />
    <?php
    }
}