• Hi,

    I have a custom post type of “event”. In this custom post type I have a custom field for an image URL. What I’m trying to do is display this image in the “all events” admin page where I’ve set my own custom columns.

    This is the code that I have now:

    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;
      }
    }

    The weird thing is that this does actually display an image, but not of the corresponding post. Instead, the same image is shown for all posts of this custom type. Not only that, the image shown is one of a regular post type, not of the “event” type.

    Here’s an image to show what it looks like now:
    https://drinksla.com/wp-content/uploads/2010/12/Screen-shot-2010-09-23-at-3.13.54-PM.png

    I hope this makes sense… I’ve been at this for hours. Thanks.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Display Images From Custom Post Field in Custom Column’ is closed to new replies.