感谢您的帮助,但该代码中的某些内容破坏了网站(可能是我们中的一个人的小疏忽)。:)
然而,我确实在另一块板上找到了一个很好的解决方案,我想在这里分享它,以帮助其他人。
贷记至helgatheviking 来自WooCommerce社区的Slack。
function insert_fb_in_head() {
global $post;
// If it is not a post or a page.
if ( ! is_singular() ) {
return;
}
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="My Website"/>\';
$gallery_image_ids = get_post_meta( $post->ID, \'_product_image_gallery\', true );
$gallery_image_ids = wp_parse_id_list( $gallery_image_ids );
// The product has product gallery, use a default image.
if( ! empty ( $gallery_image_ids ) ) {
$thumbnail_src = wp_get_attachment_image_url( current( $gallery_image_ids ), \'single-post-thumbnail\' );
echo \'<meta property="og:image" content="\' . esc_attr( $thumbnail_src ) . \'"/>\';
} else {
// The post does not have featured image, use a default image.
$default_image="https://www.website.com"; //replace this with a default image on your server or an image in your media library
echo \'<meta property="og:image" content="\' . $default_image . \'"/>\';
}
}
add_action( \'wp_head\', \'insert_fb_in_head\', 5 );
再次感谢大家。