Typecho 获取文章图片数量代码

in Typecho with 1 commentand 2595 read

做模板有些可能要显示一下文章下图片的数量,这就需要下面的代码了

/<strong><em><em></em></em></strong><em><strong><em></em></strong></em><em>*</em>
* 获取图片数量
* @since 2016.07.01
**/
function hui_post_imgNum($content){
$output =
preg_match_all("/<img.<em>?src="(.</em>?)"<sup id="fnref-1"><a href="#fn-1" class="footnote-ref">1</a></sup>*>/i", $content,$matches);
$cnt = count( $matches[1] );
return $cnt;
}

调用方法:

<?php echo ''.hui_post_imgNum($this->content).'' ; ?>

主要原理就是正则来获取图片判断几个图片

评论
  1. 这个方法你试过没,我弄后永远显示为0,有点找不到头脑,代码没错啊

    回复