我在wordpress网站上使用的 OpenGraph 代码有问题。该代码有效,但不能100%。问题在于该帖子可以正常运行-显示正确的图片,博客名称,帖子标题和说明。但是,如果我在博客的首页上检查fb共享调试器。 index.php上存在一些问题。它不会显示所选的图像(显示导航栏徽标)并显示描述,但是会显示第一篇文章的描述,而不是代码中给出的描述。我将代码添加到了底部的模板中function.php。如果有人可以提供帮助,我将非常高兴。这是下面的代码:
//Adding the Open Graph in the Language Attributes - functions.php
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:admins" content="YOUR USER ID"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="SITE NAME"/>';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="http://SITENAME/img/image.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 ), 'full' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );