I am in need of putting an array of all images attached to a post into a custom field so that I can display they on the fly using my template's existing functions.
I have gotten this real close and am successfully able to set the value of the custom meta field _et_used_images
with the contents of the array returned by get_children
, I just need some assistance getting the values returned from get_children converted/sanitized to only include the post ID of the image and not the entire image object as I am getting now:
Here is the code I am using. I have tried using a foreach loop to add to a $get_children_array[] but I am missing something and just reverted back to where it was working, but returning ALL info instead of just postID. (Example of array returned at end).
if ( 'thumbnail' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', 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) {
//Set value of et_images via attached posts.
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
//update_post_meta($post_id, '_thumbnail_id', $attachment_id); //Uncomment to generate thumbnails from attached media.
update_post_meta($post_id, '_et_used_images', $attachments);
} else {
echo __('None');
}
}
}
EXAMPLE OF RETURNED ARRAY:
a:18:{i:897;O:7:"WP_Post":24:{s:2:"ID";i:897;s:11:"post_author";s:1:"3";s:9:"post_date";s:19:"2011-08-12 05:53:33";s:13:"post_date_gmt";s:19:"2011-08-12 05:53:33";s:12:"post_content";s:153:"A grand home located in Point Loma required a grand landscaping to complement the site. this was achieved with the site planting and landscape features. ";s:10:"post_title";s:33:"1-landscape-design-point-loma-job";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"inherit";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:4:"open";s:13:"post_password";s:0:"";s:9:"post_name";s:35:"1-landscape-design-point-loma-job-3";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2011-08-12 05:53:33";s:17:"post_modified_gmt";s:19:"2011-08-12 05:53:33";s:21:"post_content_filtered";s:0:"";s:11:"post_parent";i:713;s:4:"guid";s:95:"http://example.website.com/wp-content/uploads/2011/07/1-landscape-design-point-loma-job1.jpg";s:10:"menu_order";i:2;s:9:"post_type";s:10:"attachment";s:14:"post_mime_type";s:10:"image/jpeg";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}i:896;O:7:}}
get_attached_media('image');
?get_attached_media()
does return post objects, not ids, other than that it is a good one to know and use.