我需要将所有附加到帖子的图像数组放入一个自定义字段中,以便使用模板的现有功能动态显示它们。
我已经非常接近了,并且成功地设置了自定义元字段的值_et_used_images
返回的数组内容get_children
, 我只需要一些帮助来获取从get\\u children转换/清理返回的值,以便只包括图像的post ID,而不是像我现在得到的那样包括整个图像对象:
这是我正在使用的代码。我曾尝试使用foreach循环添加到$get\\u children\\u数组[],但我丢失了一些内容,只是返回到了它的工作位置,但返回了所有信息,而不仅仅是posid。(末尾返回的数组示例)。
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\');
}
}
}
返回的数组示例:
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:}}
最合适的回答,由SO网友:AsteriskMMT 整理而成
好的,所以我最终选择了一条完全不同的路线,并且有了一个很好的工作解决方案
function update_images_meta( $object_id ) {
// Get the post using its ID
$post = get_post( $object_id );
// determine if we are dealing with a post (a parent) or an attachment (a child)
switch( $post->post_type ){
case \'attachment\':
$post_id = $post->parent;
break;
default:
$post_id = $post->ID;
break;
}
if($post_id) {
// Get all the image attachments for the post
$param = array(
\'post_parent\' => $post_id,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\'
);
$attachments = get_children( $param );
// Initialize the array
$atts = array();
// Fill the array with attachment ID\'s
foreach($attachments as $attachment)
$atts[] = $attachment->ID;
// Disable this hook which was overwrinting our changes
remove_action( \'save_post\', \'flexible_save_details\', 10 );
// Update the post\'s meta field with the attachment arrays
update_post_meta( $post_id, \'_et_used_images\', $atts );
}
}
SO网友:Nicolai Grossherr
您可以使用fields
参数的值ids
就是为了得到这些。您可以将其用于get_children()
, 它的工作原理类似于或是get_posts()
, 这也是包装器WP_Query
. 因此,您可以:
// this:
$children_images_ids = get_children(
array(
\'post_parent\' => get_the_ID(),
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'fields\' => \'ids\'
)
);
// or this:
$children_images_ids = get_posts(
array(
\'post_parent\' => get_the_ID(),
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'fields\' => \'ids\'
)
);
// or this:
$children_images_obj = new WP_Query(
array(
\'post_parent\' => get_the_ID(),
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'fields\' => \'ids\'
)
);
$children_images_ids = $children_images_obj->posts;
在任何情况下
$children_images_ids
将已经是一个ID数组,看起来有点像这样:
Array
(
[0] => 123
[1] => 456
[2] => 789
)