get_children
将返回
立柱的关联数组[…]post ID为array keys, 如果找不到帖子,则返回空数组。
本声明$attachment_id => $attachment
与更常用的相同$key => $value
.
使用此命令echo \'<pre>\'.print_r($attachments,true).\'</pre>\';
我们得到以下信息:
恢复版本的get_children
具有numberposts => 2
Array
(
[83] => stdClass Object
(
[ID] => 83
[post_author] => 1
[post_date] => 2012-06-22 05:06:22
[post_content] =>
[post_title] => barajas
[post_excerpt] =>
[post_status] => inherit
[post_name] => barajas-4
[post_modified] => 2012-06-22 05:06:22
[post_modified_gmt] => 2012-06-22 05:06:22
[post_parent] => 28
[guid] => http://wp00.dev/wp-content/uploads/barajas3.jpg
[menu_order] => 0
[post_type] => attachment
[post_mime_type] => image/jpeg
)
[180] => stdClass Object
(
[ID] => 180
[post_author] => 1
[post_date] => 2012-06-30 04:18:06
[post_content] =>
[post_title] => Yuri Atom_00281
[post_excerpt] =>
[post_status] => inherit
[post_name] => yuri-atom_00281
[post_modified] => 2012-06-30 04:18:06
[post_parent] => 28
[guid] => http://wp00.dev/wp-content/uploads/Yuri-Atom_00281.jpg
[menu_order] => 0
[post_type] => attachment
[post_mime_type] => image/jpeg
)
)
所以,
[83]
和
[180]
是
$key 和
stdClass Object
是他们的
$value.<我想打电话的时候
get_children
foreach几乎是强制性的,这将在下一步得到明确。。。
如果您使用get_posts
使用完全相同的参数,结果将完全相同,除了一个不同,返回的数组将具有数字顺序键:
Array
(
[0] => stdClass Object
(
[ID] => 83
[post_author] => 1
[post_date] => 2012-06-22 05:06:22
)
[1] => stdClass Object
(
[ID] => 180
[post_author] => 1
[post_date] => 2012-06-30 04:18:06
)
)
如果你使用
get_posts
和
numberposts => 1
, 您可以替换
foreach
阻止用于:
$thumb_for_pinterest = wp_get_attachment_image_src($attachments[0]->ID, \'full\' );
带
get_children
we don\'t know 什么是
$key
数字,因为它与附件ID相同,所以foreach。。。
但使用常规数字键(0、1、2、3…等),您可以删除foreach
并直接调用数组的第一个元素($attachments[0]
) 及其对象元素(->ID
).