INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Images from array inside another loop

Images from array inside another loop

(OP)
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

CODE --> 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

RE: Images from array inside another loop

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

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

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

RE: Images from array inside another loop

(OP)
Hmm yes, I could order them in the query if I order on title.

Thanks I will give it a go.

RE: Images from array inside another loop

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

RE: Images from array inside another loop

(OP)
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 winky smile

RE: Images from array inside another loop

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

RE: Images from array inside another loop

(OP)
Got it working, thanks for the help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close