Support » Your WordPress » Custom fields for loop

  • Hi,
    I have a gallery setup on a page, where the images are all based off of custom fields.

    $img_001 = get_post_meta($post->ID, 'img_001', $single = true);
    $img_002 = get_post_meta($post->ID, 'img_002', $single = true);

    is it possible to have the variable name number increment by one with a for loop? How can i do this.

    Any help greatly appreciated.

Viewing 1 replies (of 1 total)
  • Didn’t test this…

    //get 10 images, store in array, then loop through array and display
    for ( $counter = 1; $counter <= 10; $counter += 1) {
    $img = 'img_' . substr('000'. $counter,-3,3);
    $images[]=get_post_meta($post->ID, $img, $single = true);
    foreach ($images as $image) {
    echo "<pre>"; print_r($image); echo "</pre>";

Viewing 1 replies (of 1 total)
  • The topic ‘Custom fields for loop’ is closed to new replies.