获取wordpress文章中所有的图片及图片数量
对于做wordpress图片站设计站的站长来说,图片数量与图片显示是很必要的,通过文章列表中就可以显示出图片的数量于数量样式很提升用户体验及诉求,那我们怎样在一款主题上实现这样的功能呢,下面测速网给大家介绍下调用wordpress文章所有图片及文章所有图片数量的方法
wordpress主题获取文章中的所有图片
161718192021222324252627282930313233343536373839404142434445function hui_get_thumbnail( $single=true, $must=true ) {global $post;$html = '';if ( has_post_thumbnail() ) {$domsxe = simplexml_load_string(get_the_post_thumbnail());$src = $domsxe->attributes()->src;$src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');$html = sprintf('<li><img src="%s" /></li>', $src_array[0]);} else {$content = $post->post_content;preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);$images = $strResult[1];$counter = count($strResult[1]);$i = 0;foreach($images as $src){$i++;$src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');$src2 = $src2[0];if( !$src2 && true ){$src = $src;}else{$src = $src2;}$item = sprintf('<li><img src="%s" /></li>', $src);if( $single){return $item;break;}$html .= $item;if(($counter >= 4 && $counter < 8 && $i >= 4) ||($counter >= 8 && $i >= 8) ||($counter > 0 && $counter < 4 && $i >= $counter)){break;}}}return $html;}function hui_get_attachment_id_from_src ($link) {global $wpdb;$link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");}
wordpress主题前台调用代码放在需要显示的位置上
1<?php echo hui_get_thumbnail(false,true);?>
获取wordpress文章内容中的图片数量值
12345678if( !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]);}}wordpress前台显示代码
1<?php echo get_post_images_number().'张图片' ?>
上一篇:创建按点击阅读次数排列的wordpress热门文章列表
下一篇:wordpress企业网站快速获取高质量外链的五个方法
webacc.exe是什么文件?webacc.exe是不是病毒
WINSYS.vbs是什么文件?WINSYS.vbs是不是病毒
winssh.exe是什么文件?winssh.exe是不是病毒
wt.exe是什么文件?wt.exe是不是病毒
winsysetm.exe是什么文件?winsysetm.exe是不是病毒
winstrve.exe是什么文件?winstrve.exe是不是病毒
winsysupd7.exe是什么文件?winsysupd7.exe是不是病毒
winsysupd.exe是什么文件?winsysupd.exe是不是病毒
winsysupd2.exe是什么文件?winsysupd2.exe是不是病毒
winsysupd8.exe是什么文件?winsysupd8.exe是不是病毒