0
foreach ($array as &$value) {
    $q3 = "SELECT * FROM wp_posts WHERE post_name = '$value'";
    $r3 = $wpdb->get_results($q3);
    $Idd = $r3[0]->ID;
    $img = wp_get_attachment_url( get_post_thumbnail_id($Idd, 'thumbnail') );
    //echo $value;
    //echo $Idd;
    //echo $img;
    $list .= '<li><img src="'.$img.'"/><br>'.$value.'</li>';
}

Using the above, if I echo $value my array is printed. if I echo $Idd the result instead of several ID's is just a singular and the same goes for $img

How can I run the above to work and print out the $img's and $Idd

Thanks

3
  • Is the ampersand before $value in the foreach statement supposed to be there? Commented Mar 4, 2014 at 13:45
  • 1
    @Daniel The & is a reference operator - php.net/manual/en/language.oop5.references.php. As such, $value is a direct reference to the entry in $array, not a copy of it. Commented Mar 4, 2014 at 13:49
  • Cool thanks for letting me know, not familiar with those! Commented Mar 4, 2014 at 13:51

1 Answer 1

3

You code is loading the first value of the result set and only using it.

If you need to display all the values, then you will need to loop over the results.

foreach ($array as &$value) {
    $q3 = "SELECT * FROM wp_posts WHERE post_name = '$value'";
    $r3 = $wpdb->get_results($q3);
    foreach ($r3 as $imgRes)
    {
      $Idd = $imgRes->ID;
      $img = wp_get_attachment_url( get_post_thumbnail_id($Idd, 'thumbnail') );
      $list .= '<li><img src="'.$img.'"/><br>'.$value.'</li>';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.