// get the post object
$post = get_post( get_the_ID() );
// we need just the content
$content = $post->post_content;
// we need a expression to match things
$regex = '/src="([^"]*?)"/';
// we want all matches
preg_match_all( $regex, $content, $matches );
// reversing the matches array
$matches = array_reverse($matches);
// we've reversed the array, so index 0 returns the result
//images数量不对,仅允许提供0张,1张或者3张图
function get_Image($arr)
{
$ar = [];
$arr_lg = count($arr);
if ($arr_lg<=2) {
echo '"'.$arr[0].'"';
} else if($arr_lg>=3){
$ar = array_chunk($arr,3);
foreach($ar[0] as $k=>$v) {
// print_r($ar);
if($v != end($ar[0])) {
// print_r($v);
echo '"'.$v.'",';
}
else{
// print_r($v);
echo '"'.$v.'"';
}
}
}
}
?>
{
"@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
"@id": "<?php the_permalink();?>",
"appid": "[修改这个参数]",
"title": "<?php the_title();?>",
"images": [<?php echo get_Image($matches[0]);?>],
"pubDate": "<?php echo get_the_time('Y-m-d')."T".get_the_time('H:i:s');?>"
}