WordPress获取文章中或相册中的图片数量
时间:6年前 阅读:7700
01、小站的相册分类中文章主要有两种,一种使用的是WordPress的原生相册功能,另外一种则是普通的文章,文章内插入了图片;本文的需求就是统计文中的图片的总张数。
02、解决方案
获取文章图片数量函数
将函数post_img_number放在functions.php中。
function post_img_number(){
global $post, $posts;
ob_start();
ob_end_clean();
//使用do_shortcode($post->post_content) 是为了处理在相册的情况下统计图片张数
$output = preg_match_all('//i',do_shortcode($post->post_content), $matches);
$cnt = count( $matches[1] );
return $cnt;
}
注:本函数最大的改进是通过使用do_shortcode($post->post_content)来调用WordPress文章的内容而不是使用$post->post_content,可以解决使用原生相册功能功能时图片不能统计的问题。
调用函数
调用方法为:
<!--?php echo post_img_number().'张'; ?-->
本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。
微信扫码关注
更新实时通知


网友评论