简体   繁体   中英

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

I'm a newbie with WordPress, and I had a small problem with the Facebook open graph. After searching for more than 6 months but i'm always failing.

My site is multi-authors and all what i need to add og:image meta tag for author.php "author profile", I'm stuck and I really get tired from searching.

When the user trying to share his profile..the avatar image not coming up! because Facebook can't find the author avatar og:image!

Plugins i use : Yoast SEO , Simple Local Avatars , AddToAny Share Buttons .

Finally i found the answer and i would like to sharing it with you, but i have another small problem. Now i can get the author avatar IF he has 1 post at least!! and the authors with 0 posts Facebook can't fetch his avatar image!! Strange thing and i don't know how to fix this.

anyway this is the code i found.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM