Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently using @kovshenin's tutorial here on adding a thumbnail (a featured image) to my post type admin columns.

Would it perhaps be possible ( and how :) ) to retrieve the first image attached to a post first, if no featured image available? (and perhaps return a local hosted image if also no featured image available)

This is the code I currently have:

add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
  add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}

add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
  $column_thumbnail = array(
    'thumbnail' => ''
  );
  $column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
  return $column;
}

add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
  global $post;
  switch ( $column ) {
    case 'thumbnail':
      echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );
      break;
  }
}

Edit:

As mentioned in the next comments, it took some time to figure things out :). In the end, and with the suggestions of @ifdion I came up with the next result:

add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
  add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}

add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
  $column_thumbnail = array(
    'thumbnail' => ''
  );
  $column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
  return $column;
}

add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
  global $post;

  $args = array(
    'numberposts' => 1,
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
  );
  $attachments = get_children( $args );

  if ( has_post_thumbnail() ) {
    echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );

  } elseif ( $attachments ) {
    foreach ( $attachments as $attachment ) {
      $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'edit-screen-thumbnail' );
      echo '<img src="' . $image_attributes[0] . '">';
    }

  } else {
    echo '<img src="' . plugins_url( '/img/edit-screen-thumbnail.png', __FILE__ ) . '">';
  }
}

Another edit

Not entirely sure if this grabs the last image attached to a post, but anyways, this code will get it:

add_action( 'after_setup_theme', 'cor_after_setup_theme' );
function cor_after_setup_theme() {
  add_image_size( 'edit-screen-thumbnail', 48, 48, true );
}

add_filter( 'manage_edit-product_columns', 'cor_add_product_column', 10, 1 );
function cor_add_product_column( $column ) {
  $column_thumbnail = array(
    'thumbnail' => ''
  );
  $column = array_slice( $column, 0, 1, true ) + $column_thumbnail + array_slice( $column, 1, NULL, true );
  return $column;
}

add_action( 'manage_posts_custom_column', 'cor_manage_product_column', 10, 1 );
function cor_manage_product_column( $column ) {
  global $post;

  $args = array(
    'numberposts' => 1,
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_status' => null,
  );
  $attachments = get_posts( $args );

  if ( has_post_thumbnail() ) {
    echo get_the_post_thumbnail( $post->ID, 'edit-screen-thumbnail' );

  } elseif ( $attachments ) {
    foreach ( $attachments as $attachment ) {
      echo wp_get_attachment_image( $attachment->ID, 'edit-screen-thumbnail' );
    }

  } else {
    echo '<img src="' . plugins_url( '/img/edit-screen-thumbnail.png', __FILE__ ) . '">';
  }
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Yes it is possible. I believe it is better if you put it in a conditional statement

  1. Check for featured image easy;
  2. Check for attached file with an image mime type using this and this;
  3. Check for any <img> tag in the post content (detail);
  4. If there is no image at all, then use a default image

Hope this help

share|improve this answer
    
Sorry about the late reply, but overall it seems I'm slowly getting there :) According to one of the comments it might be better to use get_children (1) to show the first attachment of a post. Do you have any comments on this perhaps? –  Cor van Noorloos Mar 6 '12 at 17:57
    
I think that will do to, but I'm not really sure about which method to use. The Codex page itself said that "If you just want to get or display attachments, it's probably a little easier to use get_posts() instead." but onwards they do have the example for your question. –  ifdion Mar 6 '12 at 20:22
    
Thanks @ifdion! Just updated the original question. –  Cor van Noorloos Mar 6 '12 at 23:14
    
You are very much welcome ! –  ifdion Mar 7 '12 at 3:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.