PHP请求图像并用默认图像填充空白


php request for images and fill blanks with a default image

我有检索每个用户上传的图像的函数。每个用户的限制是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;
}

您必须检查当前总图像的长度:

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;
}