我运行以下内容:
// array of images
$images = get_attached_media('image' );
// The following prints each image src and srcset
// in a img element wrapper in a container div
foreach ($images as $image) {
$ximage = wp_get_attachment_image_src($image->ID,'medium');
$img_srcset = wp_get_attachment_image_srcset( $image->ID, 'medium' );
echo '<div class="col"><img class="img-fluid" src="' .$ximage[0] . '" srcset="'.$img_srcset.'"></div>';
}
跨度属性
// With the following I am trying to loop over the array
// and output each one separated with a comma as a data attribute
<span class="imgs_span" data-time="
<?php foreach($images as $value){
echo $value . ", ";};
?>">
</span>
但我什么都没得到
答案 0 :(得分:-1)
$images
var是一个对象数组,因此您应该选择所需的属性来显示它,例如:
<span class="imgs_span" data-time="<?php foreach($images as $k => $value){$attr = ($k == count($images) - 1) ? $value->post_date : $value->post_date . ", "; echo $attr;};?>"></span>
WP_Post类的完整属性列表,其中包含doc
中的描述答案 1 :(得分:-1)
如果是1D阵列,您可以使用implode()
<span class="imgs_span" data-time="<?php echo implode(",", $images); ?>"></span>
==========================
由于$images
是WP_Post类型对象的数组,因此您可以在array_map()
之前使用implode()
:
<span class="imgs_span" data-time="<?php
echo implode(",", array_map(function($item){ return $item->post_date; }, $images);
?>"></span>