我有检索每个用户上传的图像的功能。每个用户的限制是5个图像,如果上传少于5个,我想显示一个默认图像以填充最多5个空白。我怎样才能做到这一点?
function display_images() {
$imgs = get_images();
$html = '<div class="myImages">';
foreach($imgs as $img) {
$html .= '<div class="myImageContainer"><img src="'. $img .'"/></div>';
}
$html .= '</div>';
return $html;
}
function get_images() {
global $current_user;
get_currentuserinfo();
$args = array(
'author' => $current_user->ID,
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 5,
'orderby' => 'date'
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}
return $images;
}
答案 0 :(得分:2)
您必须检查总图像的当前长度:
function display_images() {
$imgs = get_images();
$html = '<div class="myImages">';
foreach($imgs as $img) {
$html .= '<div class="myImageContainer"><img src="'. $img .'"/></div>';
}
if (count($imgs)<5) {
for($i=0; $i<(5-count($imgs)); $i++) {
$html .= '<div class="myImageContainer"><img src="MY_BLANK_IMAGE_LINK"/></div>';
}
}
$html .= '</div>';
return $html;
}