某些主题会显示文章中的图片数量,发现很多人都分享过类似的文章,可是很多代码都是错的,或者都是无效的。

米扑博客结合几个类似功能的代码,经过自己的一翻折腾,居然活生生给弄出来了,不敢独享,特分享给大家:

打开主题下的函数文件 functions.php

vim functions.php

添加如下代码:

/** 获取文章中的图片数量 **/
/**
 * 参数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('/<img.*(: |\t|\r|\n)src=[\'"](.+)[\'"](:(: |\t|\r|\n)+.*)>/sim', $post->post_content, $matches ,PREG_SET_ORDER);
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/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;
    }   
}

调用方法:

<?php echo post_stat_images(0); ?>
<?php echo post_stat_images(1); ?>

 

显示效果:

米扑科技:草根连续创业的前赴后继者

 

恩,看似图片匹配上了,但是对不对呢?

实际应用中,发现还是有问题的,例如 img 中有换行、特殊符号、转义符、乱码等等

下面是我在米扑博客中,无意中遇到的一个实际问题,访问博客:创业邦——IT界风云人物

正文有一个图片是这样的,一直都匹配不上,即存在多重换行

$content= '<img alt="business-person-of-the-state-it-11" 
class="alignnone size-full wp-image-17207" 
src="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11.png" 

style="
height:100%; max-height:540px; max-width:868px; " 
width="100%" srcset="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11.png 868w, 
https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11-300x186.png 300w" 
sizes="(max-width: 868px) 100vw, 868px">


<img alt="business-person-of-the-state-it-22" class="alignnone size-full wp-image-17208" 
src="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22.png" 
style="height:100%; max-height:558px; max-width:868px; " width="100%" 
srcset="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22.png 868w, 
https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22-300x192.png 300w" 
sizes="(max-width: 868px) 100vw, 868px">';

贴上试过的方案

<?php
/**
 * WordPress 获取文章中图片数量
 * 
 * blog.mivmp.com
 * 2017.10.25
 */


@$content = '<img alt="business-person-of-the-state-it-11" 
class="alignnone size-full wp-image-17207" 
src="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11.png" 

style="
height:100%; max-height:540px; max-width:868px; " 
width="100%" srcset="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11.png 868w, 
https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-11-300x186.png 300w" 
sizes="(max-width: 868px) 100vw, 868px">


<img alt="business-person-of-the-state-it-22" class="alignnone size-full wp-image-17208" 
src="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22.png" 
style="height:100%; max-height:558px; max-width:868px; " width="100%" 
srcset="https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22.png 868w, 
https://blog.mimvp.com/wp-content/uploads/2010/07/business-person-of-the-state-it-22-300x192.png 300w" 
sizes="(max-width: 868px) 100vw, 868px">';



function post_stat_images($flag, $content){
	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('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $matches, PREG_PATTERN_ORDER);  
	$output = preg_match_all('/<img.*(: |\\t|\\r|\\n)src=[\'"](.+)[\'"](:(: |\t|\r|\n)+.*)>/sim', $content, $matches ,PREG_PATTERN_ORDER);
	$output = preg_match_all('/<img.*(: |\t|\r|\n)src=[\'"](.+)[\'"](:(: |\t|\r|\n)+.*)>/sim', $content, $matches ,PREG_PATTERN_ORDER);
	$output = preg_match_all('/<img.*(: |\\t|\\r|\n)src=[\'"](.+)[\'"](:(: |\\t|\\r|\\n)+.*)>/sim',$post->post_content,$matches ,PREG_SET_ORDER);
	*/
	
// 	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches, PREG_SET_ORDER);	// 不支持换行
// 	$output = preg_match_all('/<img(.+\r*\n*\d*)src=[\'"]([^\'"]+)[\'"].*/i', $content, $matches, PREG_SET_ORDER);	// 支持换行, 去掉了img结尾符 >
	
	// 先替换换行符在匹配,结果多个img合并到一行,出人意料的只匹配一个img
// 	$content = preg_replace("/([\r\n|\n|\t|]+)/", '', $content);
// 	echo "content : " . $content . "\n\n";
// 	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches, PREG_SET_ORDER);
	

	$output = preg_match_all('/<img.+/i', $content, $matches, PREG_SET_ORDER);		// 最终解决方案,简单粗暴
	
	print_r($matches);
	
	$post_imgs_count = count( $matches );
	
	if($flag==1){
		$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;
	}
}

echo post_stat_images(0, $content);
echo post_stat_images(1, $content);
?>

 

 

WordPress获取所有图片的数量

上面的方法是WordPress获取文章中的图片数量,是针对文章,

如果要输出所有文章的图片数量呢,可以通过下面的代码来统计网站里的图片。

function img_count(){   
	$query_img_args = array(   
		'post_type' => 'attachment',   
		'post_mime_type' =>array(   
			'jpg|jpeg|jpe' => 'image/jpeg',   
			'gif' => 'image/gif',   
			'png' => 'image/png',   
		),   
		'post_status' => 'inherit',   
		'posts_per_page' => -1,   
	);   
	$query_img = new WP_Query( $query_img_args );   
	echo $query_img->post_count;   
} 

添加到functions.php中

<?   img_count();   ?>

 

最后,展示下我的米扑博客管理后台,显示的每篇文章图片数量

实现原理,参考本文和米扑博客之前发表的博客:WordPress 后台文章列表添加自定义列

阅读数量Views 和 百度收录状态,原理其实都一样

认真研究本文和参考推荐,都很容易实现

 

 

参考推荐:

WordPress 百度收录检测与后台文章显示收录状态

WordPress 后台文章列表添加自定义列