Reputation: 1
My posts have 3 images attached, I want to turn the 3 image urls into 3 separate variables and use them background images in css
. So far I have been able to display the 3 urls, but cannot turn them into variables so I can call them as background image.
if ($attachments = get_children(array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
$mynewarray = wp_get_attachment_image_src($attachment->ID, 'rig');
$anotherarray = $mynewarray[0];
echo $anotherarray ;
}
The result of above is here.
How can I separate these and turn them into variables, like $backgroundimage1, $backgroundimage2 & $backgroundimage3
. So i can call them later in page, like below
<div class="backgound-image" style="background-image: url(<?php echo $backgroundimage1 ?>)">
Upvotes: 0
Views: 97
Reputation: 433
You can add all attachments to new array. And get indexes from array same you want.
if ($attachments = get_children(array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
)));
$mynewarray = array();
foreach ($attachments as $attachment) {
$mynewarray = wp_get_attachment_image_src($attachment->ID, 'rig');
$anotherarray[] = $mynewarray[0];
// echo $anotherarray ;
// not echo, only define in here
}
After defination, you can get these.
echo $mynewarray[0]; // or [1] or [2]
Upvotes: 2