我正在为一位客户建造一个定制旋转木马。我有一个函数,可以从贴在帖子上的所有图像中获取三幅图像的块:
global $rental;
$images = get_children( array(
\'post_parent\' => $rental_id,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\'
) );
$array = $images;
$number_of_elements = 3;
$count = count( $array );
$split = array();
for ( $i = 0; $i <= $count - 1; $i++ ) {
$slices = array_slice( $array, $i , $number_of_elements);
if ( count( $slices ) != $number_of_elements )
break;
$split[] = $slices;
}
if ($split) :
foreach ($split as $outeritem) :
echo \'<div class="Outer Top">\';
foreach ($split as $inneritem) :
echo \'<div class="Inner Top">\';
echo \'<img src="\' . $inneritem . \'">\';
echo \'</div>\';
endforeach;
echo \'</div>\';
endforeach;
endif;
//print_r( $split );
我要做的就是更换
inneritem
使用图像的URL。数据都在一个数组中,正如您所看到的,我只需要为每个项目提取guid的值。下面的数组来自取消注释
print_r( $split );
为了保持整洁,我删除了所有无关数据:
Array (
[0] => Array (
[0] => WP_Post Object (
[ID] => 120
[guid] => http://******/wp-content/uploads/2016/12/T15923-11-1-1.jpg
)
[1] => WP_Post Object (
[ID] => 121
[guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
)
[2] => WP_Post Object (
[ID] => 122
[guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
)
)
[1] => Array (
[0] => WP_Post Object (
[ID] => 121
[guid] => http://******/wp-content/uploads/2016/12/T15923-12-1-1.jpg
)
[1] => WP_Post Object (
[ID] => 122
[guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
)
[2] => WP_Post Object (
[ID] => 123
[guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
)
)
[2] => Array (
[0] => WP_Post Object (
[ID] => 122
[guid] => http://******/wp-content/uploads/2016/12/T15898.jpg
)
[1] => WP_Post Object (
[ID] => 123
[guid] => http://******/wp-content/uploads/2016/12/T15923-13-1-1.jpg
)
[2] => WP_Post Object (
[ID] => 124
[guid] => http://******/wp-content/uploads/2016/12/T15923-14-1.jpg
)
)
)
最合适的回答,由SO网友:Tunji 整理而成
你应该能够重写你所拥有的,并使用get_permalink
像@Benoti 在省略$split
大堆
get_permalink
接受Post ID或Post对象。
global $rental;
$images = get_children( array(
\'post_parent\' => $rental_id,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'order\' => \'ASC\',
\'orderby\' => \'menu_order ID\'
) );
$array = $images;
$number_of_elements = 3;
$count = count( $array );
for ( $i = 0; $i <= $count - 1; $i++ ) {
$slices = array_slice( $array, $i , $number_of_elements);
if ( count( $slices ) != $number_of_elements )
break;
echo "<div class=\'Outer Top\'>";
foreach( $slices as $inneritem ) {
$link = wp_get_attachment_url( $inneritem->ID );
echo "<div class=\'Inner Top\'>";
echo "<img src=\' $link \'>";
echo "</div>";
}
echo "</div>";
}