Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:49:03+00:00 2026-05-30T23:49:03+00:00

Ive cobbled together the following code and it works, but it feels too hacky:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:49:05+00:00Added an answer on May 30, 2026 at 11:49 pm

    Much simpler to do:

    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'
        ))){
    
        $img_med = wp_get_attachment_image($imgs[0]->ID,'medium');
        $img_larg = wp_get_attachment_image_src($imgs[0]->ID,'large');
    
        echo '<a class="myimage" href="' . $img_larg[0] .'">' . $img_med .'</a>';
    } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the code below (from a console app I've cobbled together), I add seven
I've written (well cobbled together from other people's code) a very simple uptime monitor
Ive got a following problem. I made a XUL toolbar. Toolbar works correctly, it
I've cobbled together a C# program that takes a .csv file and writes it
$(document).ready(function() { $(span.link).mouseover(function(e){ $(this.children).css(display,inline); }); }); I'm not a javascript expert, but I've cobbled
Ive narrowed down to this method but i don't understand why its locking the
I admit I've cobbled together a mostly working production setup on Ubuntu with Capistrano
This is probably a noob question, but how I've implemented the appendTo() function below
I've cobbled together a Windows Class Library (Called MPC_Core) using .NET 4, I'm referencing
I have a div I've made resizable using jQuery UI. It works fine, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.