[英]WordPress - og:image open graph to fetch the authors avatar
我是WordPress的新手,使用Facebook開放圖時遇到了一個小問題。 搜索超過6個月后,但我總是失敗。
我的網站是多作者的,我需要為author.php“作者個人資料”添加og:image元標記,我被困住了,我真的對搜索感到厭倦。
當用戶嘗試分享他的個人資料時,頭像圖片不出來! 因為Facebook找不到作者頭像og:image!
我使用的插件: Yoast SEO , 簡單的本地頭像 , AddToAny共享按鈕 。
終於,我找到了答案,並且想與您分享,但是我還有另一個小問題。 現在,如果他至少有1個帖子,我可以得到作者的頭像!! 0個帖子的作者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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.