wordpress 调用文章第一张图片url并返回文章总图片数量代码

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

/** 获取文章中的图片数量 **/
/**
* 参数flag=0,返回图片数量
* 参数flag=1,返回全部图片src链接
*
* blog.mimvp.com
* 2016.12.25
*/
function post_stat_images($flag){
global $post;
$post_imgs_count = 0;

// post_imgs_count = get_post_meta($post_id, 'post_imgs_count', true);

ob_start();
ob_end_clean();
// $output = preg_match_all('//sim', $post->post_content, $matches ,PREG_SET_ORDER);
$output = preg_match_all('//i', $post->post_content, $matches, PREG_SET_ORDER);
$post_imgs_count = count( $matches );

if($flag==1){
// // 取第一张图片src地址
// $post_imgs_src = $matches [0][1];

// 遍历所有图片src地址
$post_imgs_srcArray = array();
foreach($matches as $match) {
$img_src = $match[1];
array_push($post_imgs_srcArray, $img_src);
}
$post_imgs_src = implode(',',$post_imgs_srcArray);
return $post_imgs_src;
}else{
return $post_imgs_count;
}
}

调用方法:

以上代码来自米朴博客