最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress 获取文章内所有图片个数与图片地址例子
时间:2015-06-08 编辑:简简单单 来源:一聚教程网
WordPress获取文章中的图片个数
复制以下代码到当前使用主题的functions.php文件中,然后在文章列表主循环或文章页中调用该函数即可。
注:xiu主题已有该功能。
WordPress获取文章中的图片个数函数
/*
* 获取文章中的图片个数 (使用在文章列表主循环中、或文章页中)
*/
if( !function_exists('get_post_images_number') ){
function get_post_images_number(){
global $post;
$content = $post->post_content;
preg_match_all('/
return count($result[1]);
}
}
函数使用方法:
wordpress 获取文章内所有图片
将代码插入functions.php
function 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('
} else {
$content = $post->post_content;
preg_match_all('/
$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('', $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实现文章分页功能例子
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31