Treehouse: Grow your CSS skills. Land your dream job.

Auto thumbnail

  • # April 27, 2010 at 11:54 am

    Hey duderinos,

    I’ve bashed and crowbar-ed the following together: when you create a WP post, you may want to use a thumbnail image somewhere – beside the title, for example.

    You can do this using a custom field of course, but with the following if you don’t specify a thumbnail image to use then the first image you associate with that post will then automatically become the thumbnail instead.

    < ?php global $wpdb; $attachment_id = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_parent = ‘$post->ID’ AND post_status = ‘inherit’ AND post_type=’attachment’ ORDER BY post_date ASC LIMIT 1″); ?>

    < ?php $attachment_image = wp_get_attachment_url($attachment_id); ?>

    < ?php $thumbpath = get_post_meta($post->ID, ‘thumbnail’, true);
    if($thumbpath ==””) {
    $thumbpath = $attachment_image;


    Viewing 1 post (of 1 total)

    You must be logged in to reply to this topic.


    CSS-Tricks* is created, written by, and maintained by Chris Coyier. It is built on WordPress, hosted by Media Temple, and the assets are served by MaxCDN. The fonts are Source Sans and Source Code Pro. It is made possible by viewers like you who subscribe to The Lodge and through advertising for products and services I like.

    *May or may not contain any actual "CSS" or "Tricks".