下面是获取项目列表、返回第一个项目并显示第一个id中的单个图像的四个示例。
explode// get your custom field data (this looks like your meta_value)
$meta_value = "378,377,376,375,374,373";
// convert csv to array
$ids = explode ( \',\', $meta_value );
// pull the first item out as the variable $id
list($id) = $ids;
// show the image
echo wp_get_attachment_image ( $id, \'medium\' );
<小时>
get_attached_media// assume you have these vals
$post = get_post();
$post_id = $post->ID;
// get the attachments
$attachments = get_attached_media(\'image\', $post_id);
if(count($attachments) > 0) {
$first_attachment = array_shift($attachments);
echo wp_get_attachment_image($first_attachment->ID);
}
<小时>
get_post_meta$post = get_post();
$post_id = $post->ID;
$key = \'attachment\';
$generate_if_not_found = false; // for testing
if($generate_if_not_found && empty(get_post_meta($post_id, $key, true))) {
// write the values if we don\'t have them
update_post_meta($post_id, $key, array(386, 393, 234, 23, 23, 234));
}
$ids = get_post_meta($post_id, $key, true);
if( ! empty($ids) && count($ids) > 0) {
$first_id = array_shift($ids);
echo wp_get_attachment_image($first_id);
}
<小时>
get_post_custom_values$post = get_post();
$post_id = $post->ID;
$key = \'attachment\';
if(is_array($value = get_post_custom_values($key, $post_id))) {
if(count($value) > 0) {
if(is_array($ids = maybe_unserialize($value[ 0 ]))) {
if(count($ids) > 0) {
$id = array_shift($ids);
echo wp_get_attachment_image($id);
}
}
}
}
<小时>
Custom Column
function posts_render_custom_columns($column_name, $post_id) {
if($column_name === \'attachment\') {
$size = \'thumbnail\';
$attachments = get_attached_media(\'image\', $post_id);
if(count($attachments) > 0) {
$first_attachment = array_shift($attachments);
echo wp_get_attachment_image($first_attachment->ID, $size);
}
}
}