WordPress 列表调用文章所有图片及数量显示

2018年5月30日 2643点热度 3人点赞 1条评论

WordPress 列表调用文章所有图片及数量显示

将下面的函数加入主题模板 functions.php 文件中

WordPress 获取文章中的图片个数函数


/* 
 * 获取文章中的图片个数 (使用在文章列表主循环中、或文章页中) 
 */ 
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().'张图片' ?>

WordPress 获取文章内所有图片

WP函数

源码下载:点击这里

调用方法:<?php echo hui_get_thumbnail(false,true);?>

出处:爱找主题

WPTalk

这个人很懒,什么都没留下