Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images from array inside another loop

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
Hi

I am using Wordpress and and looping through posts of a particular type, but wish to show a different image for each post from a list of images uploaded to the media library

Here's what I am doing

PHP:
//create my array of images with a particular title (such as myimages, myimages1, myimages823746782)
$query_images_args = array(
'post_type'      => 'attachment',
'post_mime_type' => 'image',
'post_status'    => 'inherit',
's' => 'myimages',
'posts_per_page' => - 1,
);

$query_images = new WP_Query( $query_images_args );

$images = array();
foreach ( $query_images->posts as $image ) {
    $images[] = wp_get_attachment_url( $image->ID );
}

//then loop through my posts
$latestjobs = $wpdb->get_results("SELECT ...");

foreach ( $latestjobs as $latestjob ) 
 {
   echo '<div>';
   echo '<h2>'.$latestjob->post_title.'</h2>';
   echo '<img src='..' />'; //this is where I want each to show a different image from my array, how do I do that?
   echo '</div>';
 }

How do I get a get the images out of my array, one for each post in the loop? If there are more posts than images, we start back at the first etc

Thanks
 
Assuming the image_ids are in the order you want them and the path to the image is built from the image ids in the array, just construct the src href from your ID array.

You'll need to keep a counter to use in your images array.

Code:
[b]$img_cntr = 0;[/b]
foreach ( $latestjobs as $latestjob ) 
 {
   echo '<div>';
   echo '<h2>'.$latestjob->post_title.'</h2>';
   echo '<img src='. [b]$images[$img_cntr][/b] .' />'; //this is where I want each to show a different image from my array, how do I do that?
   echo '</div>';
[indent][b]$img_cntr++;[/b][/indent]
 }

So each time your foreach loop runs, the counter is increased and the next image in your image array is used for the src of the image tag.


----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Hmm yes, I could order them in the query if I order on title.

Thanks I will give it a go.
 
If they are not ordered, then you need to know what image is in what position of the array, and how it relates to the job you are displaying in the loop. But you would need to access the array then by index number.

Or create your array so it has some identification method of the images in relation to the jobs.

How do you determine which image goes to what job?

Your array, could be something like $images['job_number'] = image_id. or something like that.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
The problem is that I don't create the images and going forwards the people creating them might not put them in numerical order so the array position may be a more robust solution for me.

It doesn't matter which image gets assigned to what job, as long as each has a different one (or then loping around as long as they are far enough apart that it doesn't look like obvious repetition). All images should be appropriate images for the page - in theory ;-)
 
In that case, then it really does not matter in what order they end up in the array. The code above should generally work to grab an image id from the array on each foreach loop iteration.

If there are less images than total jobs, then you'll have to reset the counter once you reach the max image array elements and start back at 0.



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Got it working, thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top