Ive cobbled together the following code and it works, but it feels too hacky:
function get_my_img() {
if($imgs = get_posts(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'DESC'
))){
foreach($imgs as $img) {
$img_med = wp_get_attachment_image($img->ID,'medium');
}
foreach($imgs as $img) {
$img_larg = wp_get_attachment_image($img->ID,'large');
}
if (preg_match('/<img.+?src(?: )*=(?: )*[\'"](.*?)[\'"]/si', $img_larg, $img_r)){
echo '<a class="myimage" href="' . $img_r[1] .'">' . $img_med .'</a>';}
}
}
I’m pretty sure there is a far simpler way to do this that I’ve completely overlooked.
In essence, when this function is called inside the loop, it outputs the last image (attached) in the current post in it’s medium size wrapped in a link to its large size.
Any insight Would be greatly appreciated.
Much simpler to do: