I’m afraid that your code doesn’t make much sense (especially the part with foreach loop…), but here’s how it can look like…
$attachments = get_children( array(
'numberposts' => -1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $posid,
'post_type' => 'attachment',
) );
// print image 0
if ( array_key_exists( 0, $attachments ) ) {
$mimg = wp_get_attachment_image_src( $attachments[0]->ID, 'large' );
$msrc = $mimg[0];
$mw = $mimg[1];
$mh = $mimg[2];
$atslug = $at->post_name;
$aturl = $surl . "https://wordpress.stackexchange.com/" . $poslug . "https://wordpress.stackexchange.com/" . $atslug . "https://wordpress.stackexchange.com/";
echo '<img class="mdi" src="' . $msrc . '" />';
}
// print image <NUMBER>
if ( array_key_exists( <NUMBER>, $attachments ) ) {
$mimg = wp_get_attachment_image_src( $attachments[<NUMBER>]->ID, 'large' );
$msrc = $mimg[0];
$mw = $mimg[1];
$mh = $mimg[2];
$atslug = $at->post_name;
$aturl = $surl . "https://wordpress.stackexchange.com/" . $poslug . "https://wordpress.stackexchange.com/" . $atslug . "https://wordpress.stackexchange.com/";
echo '<img class="mdi" src="' . $msrc . '" />';
}
PS. I’m not entirely sure what this part of code is for:
$msrc = $mimg[0];
$mw = $mimg[1];
$mh = $mimg[2];
$atslug = $at->post_name;
$aturl = $surl . "https://wordpress.stackexchange.com/" . $poslug . "https://wordpress.stackexchange.com/" . $atslug . "https://wordpress.stackexchange.com/";
echo '<img class="mdi" src="' . $msrc . '" />';
because you don’t use most of these variables ($mw, $mh, $atslug, $aturl), but I guess it can be only part of your code, so I’ll leave them… But if you really don’t use them, then you should remove them from that code…
Hope it’ll be helpful 🙂