WordPress.org

Support

Support » Plugins and Hacks » Hacks » How do I display an image from a custom field in a custom post type column?

How do I display an image from a custom field in a custom post type column?

  • fcarthy

    @fcarthy

    I have a custom post type, and I have also modified the columns in the admin panel to show certain fields. The only problem is that when I try to pull the image from a custom field I created all posts pull the same image. The funny part is that the image that is pulled is always the first image that was uploaded to ANY post type. This is what I have:

    add_filter("manage_edit-event_columns", "events_edit_columns");
    add_theme_support( 'post-thumbnails', array( 'event' ) );
    
    function events_edit_columns($columns){
      $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Event Title",
        "date" => "Event Date",
        "drink_name" => "Drink",
        "venue_name" => "Venue",
        "drink_image" => "Drink Image",
      );
    
      return $columns;
    }
    function events_custom_columns($column){
      global $post;
    
      switch ($column) {
        case "drink_name":
          $custom = get_post_custom();
          echo $custom["drink_name"][0];
          break;
        case "venue_name":
          $custom2 = get_post_custom();
          echo $custom2["venue_name"][0];
          break;
        case "drink_image":
    			$width = (int) 50;
    			$height = (int) 50;
    				$thumbnail_id = get_post_meta( $post_id, 'drink_image', true );
    				// image from gallery
    				$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
    				if ($thumbnail_id)
    					$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
    				elseif ($attachments) {
    					foreach ( $attachments as $attachment_id => $attachment ) {
    						$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
    					}
    				}
    					if ( isset($thumb) && $thumb ) {
    						echo $thumb;
    					} else {
    						echo __('None');
    					}
          break;
      }
    }

    Here’s an example:
    http://drinksla.com/wp-content/uploads/2010/12/Screen-shot-2010-09-23-at-3.13.54-PM.png

  • The topic ‘How do I display an image from a custom field in a custom post type column?’ is closed to new replies.