雄厚wordpress主题的info tags 除了分类、标签、作者、宣布时候等等外,实在,我们也能够放出文章内的图片数目,那末本日说的就是wordpress猎取文章中的图片个数的教程!
复制以下代码到当前运用主题的functions.php文件中,然后在文章列表主轮回或文章页中挪用该函数即可。
//猎取文章中的图片个数
if( !function_exists('get_post_images_number') ){
function get_post_images_number(){
global $post;
$content = $post->post_content;
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $result, PREG_PATTERN_ORDER);
return count($result[1]);
}
}
函数运用方法:
<?php echo get_post_images_number().'张图片' ?>

评论列表