【发布时间】:2011-07-07 17:10:00
【问题描述】:
我在 WP 中有帖子。这篇文章已附上图片作为附件。我通过帖子编辑部分的对话框(图库选项卡)设置了此图像的描述。
现在我需要 WP 函数以编程方式获取此附件的所有元数据(描述、标题、标题、...)和另一个函数来设置相同的数据。
使用什么功能?
【问题讨论】:
我在 WP 中有帖子。这篇文章已附上图片作为附件。我通过帖子编辑部分的对话框(图库选项卡)设置了此图像的描述。
现在我需要 WP 函数以编程方式获取此附件的所有元数据(描述、标题、标题、...)和另一个函数来设置相同的数据。
使用什么功能?
【问题讨论】:
使用 get_children() 检索帖子的附件。
$args = array(
'numberposts' => -1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
这里有一个完整的例子Get URLs, Captions & Titles for Images Attached to Posts in WordPress
【讨论】:
这对我有用:
<?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
数组的顺序分配如下。
$src[0] => url
$src[1] => width
$src[2] => height
$src[3] => icon
【讨论】: