How to get and set image attachment metadata in wordpress?
I have post in WP. This post has attached image as attachment. I set description of this image via dialog (gallery tab) in post edit section.
Now I need WP functions to programmatically get all metadata for this attachment (description,title,caption,...) and another function to set the same da开发者_Go百科ta.
What functions to use ?
use get_children() to retrive atachement for posts.
$args = array(
'numberposts' => -1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
a full example here Get URLs, Captions & Titles for Images Attached to Posts in WordPress
This works for me:
<?php
foreach ( $attachments as $attachment_id => $attachment ) {
$src = wp_get_attachment_image_src( $attachment_id, 'full' );
var_dump($src);
} ?>
array
0 => string 'http://example.com/wp-content/uploads/2009/08/DSC00261.JPG' (length=63)
1 => int 1632
2 => int 1224
3 => boolean false
The order of the array is allocated as follows.
$src[0] => url
$src[1] => width
$src[2] => height
$src[3] => icon
精彩评论