wordpress百度熊掌號「搜索結果出圖」改造代碼

<?php
if(is_single()||is_page()){
	echo '<script type="application/ld+json">{
	"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
	"@id": "'.get_the_permalink().'",
 	"appid": "這裏請填寫熊掌號 ID",
	"title": "'.get_the_title().'",
	"images": ["'.fanly_post_imgs().'"],
	"pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
}</script>
';}
//獲取文章中的圖 last update 2018/01/22
function fanly_post_imgs(){
	global $post;
	$src = '';
	$content = $post->post_content;  
	preg_match_all('/<img .*?src=[\"|\'](.+?)[\"|\'].*?>/', $content, $strResult, PREG_PATTERN_ORDER);  
	$n = count($strResult[1]);  
	if($n >= 3){
		$src = $strResult[1][0].'","'.$strResult[1][1].'","'.$strResult[1][2];
	}elseif($n >= 1){
		$src = $strResult[1][0];
	}
	return $src;
}
?>

這段代碼放在主題的header.php文件的</head>標籤以前,設置APPID便可。javascript

須要注意的是:php

圖片尺寸比例3:2,圖片大小不得低於300*200px;java

圖片必須出如今文章正文中,且按順序自動提取圖片;json

同一個網頁中,圖片不能重複;微信

圖片內容不得低俗,不能出現二維碼、電話、微信號等受益方式;app

相關文章
相關標籤/搜索