jetpack og:image

文章分享到微博的时候就会出现下面的空白图片,检查了一下数据是wp的cdn加速中的文件:

看了下网页的源代码,同样可以找到这个图片。









猜测可能是由于wp的社交分享功能导致的,使用jetpack og:image 进行搜索,找到了下面的文章:

https://iblog.ph-wbc.com/jetpack-fb-分享修改-ogimage/

通过文章中的代码即可修改og:image属性, 代码如下(将下面的代码添加到当前主题目录下的functions.php文件末尾即可):

文章分享到微博的时候就会出现下面的空白图片,检查了一下数据是wp的cdn加速中的文件:

看了下网页的源代码,同样可以找到这个图片。

猜测可能是由于wp的社交分享功能导致的,使用jetpack og:image 进行搜索,找到了下面的文章:

Jetpack FB 分享修改 og:image
通过文章中的代码即可修改og:image属性, 代码如下:
function get_feature_image(){
	global $post, $posts;
	$first_img = '';
	if ( has_post_thumbnail() ) {
		$first_img = wp_get_attachment_url( get_post_thumbnail_id() );
	} else {
		ob_start();
		ob_end_clean();
		$output = preg_match('/< *img[^>]*src *=*["\']?([^"\']*)/i', $post->post_content, $matches);
		$first_img = $matches[1];
	}
	return $first_img;
}
function fb_home_image( $tags ) {
	if ( is_home() || is_front_page() ) {
		// Remove the default blank image added by Jetpack
		unset( $tags['og:image'] );
		unset( $tags['og:description'] );
		//$fb_home_img = get_bloginfo('template_directory').'/img/default.jpg';
		$fb_home_image = 'https://h4ck.org.cn/wp-content/uploads//2019/09/logo3.png';
		$tags['og:image'] = esc_url( $fb_home_img );
		$tags['og:description'] = get_bloginfo('description');
	} elseif(is_single( ) ){
		unset( $tags['og:image'] );
		$tags['og:image'] = get_feature_image();
	}
	return $tags;
}
add_filter( 'jetpack_open_graph_tags', 'fb_home_image' );

修改之后再次分享,图片就可以正常显示了:

 

☆版权☆

* 网站名称:obaby@mars
* 网址:https://lang.ma/
* 个性:https://oba.by/
* 本文标题: 《jetpack og:image》
* 本文链接:https://nai.dog/2020/03/7002
* 短链接:https://oba.by/?p=7002
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注