Spruce interactive

Noise

Display Featured Images in WordPress with the First Attachment Image as Backup

Posted: June 8th, 2012

Author: KJ Parish

Tags:

,

 Details

We pulled together this script during a redesign for a client with an existing WordPress site. The client had a LOT of archived articles and posts where the images were included within the post, but the initial site was developed long before the days of post thumbnails. This is where this handy little script comes in. By using this, we can tell the page to display the featured image (post thumbnail) if it exists. If not, display the first image attached to the post. No need to edit the database or install any plugins to make this conversion happen.

Add to the loop: 

 <?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
 the_post_thumbnail('article-thumb');
} else {
  echo main_image(); } ?>

Add to functions.php 

 // display the first image attachment
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
    $template=get_template_directory();
    $the_title=get_the_title();
    print "<img src='$main' alt='$the_title' class='frame' />";
  endif;
}

  • Penelope

    Hi, this is great. Is there a way to rewrite this so that it pulls the first image that appears in the post as opposed to the first image attached to post? Thank you.

    • spruceit

      Hi Penelope–glad to help! You can go into the main_image() function and add an ELSE statement. We’ve done this on a few sites, and based our solution on Chris Coyier’s: http://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post. If you’re having any trouble putting it all together let us know and we’ll send over the full function for you to use.

Talk to Us

Wise men have said, "All good things must come to an end", and so it was with Spruce. We began our first project in 2006, and completed our last in Fall 2013. It's been a wonderful experience, but now it's time to begin the next adventure.

Though...we wouldn't want to leave you hanging, now would we? Here are our top recommendations for world-class branding, design, and web development:

  • Kristina (KJ) Parish and Beam Collective:
    KJ provided the artistic vision that allowed Spruce to succeed, and we still think she's the greatest designer in the entire world. She's started a new collective with some talented developers, and is covering much the same territory as Spruce--design and development all under one roof, with a caliber of service and professionalism seldom seen in this business or any other. If you like what you see on the Spruce site, then definitely get in touch with KJ at kj@beamcollective.com
  • Permanent Art & Design
    Permanent is a nationally recognized design & branding firm out of Minneapolis, MN. They've been a long term collaborator and strategic partner over the past couple years, and we'd recommend them for medium to large sized businesses/organizations who are serious about putting their best foot forward. In addition to design, Permanent offers strategy and marketing services, plus in-house and networked developers. For more information contact Joseph Belk joseph@permanentadg.com
  • Nate Thompson
    A freelancer, Nate reminds us of ourselves back in the day. Smart, communicative, and skilled, Nate is our recommendation for small to medium sized businesses/organizations who have sites built on Wordpress or other open source content management solutions. With a background in both design and development, he's a one-stop shop for most web-related tasks. You can get in touch at nate.a.thompson@gmail.com
  • Kevin DeBernardi
    Designer turned developer, Kevin is embarking on a freelance web career after working as the in-house designer at the Museum of the City of New York. He's adept at translating his design ideas into custom PHP code, and is constantly expanding his technical palette. Kevin's a good choice for projects that don't quite fit into the Wordpress mold, and that would benefit from a consistency of presence and vision from design through execution. Kevin can be reached at kevin@analoglifestyle.com