I’m building a custom carousel for a client. I’ve got a function which gets blocks of three images from all of the images attached to a 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 );
$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 );
All I need to finalize this is to replace inneritem
with the URL of the image. The data is all there in an array, and as you can see I just need to pull the value of guid for each item. The array below comes from uncommenting the print_r( $split );
and I’ve removed all the extraneous data for the sake of tidiness:
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
)
)
)
2 Answers
You should be able to rewrite what you have about and also use get_permalink
as @Benoti stated while omitting the $split
array.
get_permalink
accepts either the Post ID or a post object.
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="https://wordpress.stackexchange.com/questions/251866/$link">";
echo "</div>";
}
echo "</div>";
}