我在functions.php(wordpress)中添加了以下代码,以便动态添加元数据,因为它在每个页面上都不同。
//facebook opengraph
//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'add_opengraph_doctype');
//Lets add Open Graph Meta Info
function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
echo '<meta property="fb:app_id" content="???"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '?fbrefresh=CAN_BE_ANYTHING"/>';
echo '<meta property="og:site_name" content="MY SITE"/>';
echo '<meta property="og:image:width" content="100"/>';
echo '<meta property="og:image:height" content="100"/>';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="www.mysite.com/wp-content/uploads/2017/04/Marine-1.jpg"; //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
之前我直接在标题中添加了Open Graph元数据,这对于测试目的而言不是动态的。
现在,当我在调试器中废弃新元数据时,它会显示正确的元数据。但是,当我点击我网站上的分享按钮时,它仍会显示不再存在的旧元数据。
即使页面源显示正确/最新的元数据,但共享者仍然显示旧的,不存在的元数据。
如何确保在我点击分享按钮的每个页面上获取新的元数据?
答案 0 :(得分:0)
您必须向FB api发送缓存清除请求。但它花了很多 例如。通过卷曲(但花了很长时间)
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'id='.$url.'&scrape=true'); //site url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
$curl_results = curl_exec($curl);
curl_close($curl);
也许可以通过javascript试试