I've a series of custom fields attached to certain pages. These fields are named slide1, slide2, side3, slide4, slide5, slide6.

These are images which are going to be used as a slideshow (I'm using jflow slider for this - http://net.tutsplus.com/articles/news/using-the-wonderful-jflow-plugin-screencast/ )

(I don't want to call them all 'slide' and put them in an custom field array because there may be captions for each slide, and when both slides and captions are custom field arrays, the order may not match up)

Now, sometimes there will be only 3 slides entered (slide1 to slide3), and sometimes all 6 slides (slide1 to slide6).

Not being very PHP savvy, I wonder what the best way best way to stick these custom fields into a PHP array, and then iterate thru them to print out the code, which looks a bit like the below?

<div id="slides">  
 <div><img src="qwerty.jpg">  
 <div><img src="foo.jpg">  
 <div><img src="test.jpg">   

The code to print, for instance slide1's image, would be something like:

<img src="<?php echo wp_get_attachment_url($slide1); ?>">

2 Answers 2


This way of wokring with slides seems to be as popular as it is terrible... :) I coded something very much like this recently.

Basic idea, adjust as needed:

$id = get_the_ID();
$slides = array();
$i = 1;    

foreach( get_post_custom_keys( $id ) as $key )
    if ( false !== strpos( $key, 'slide' ) )
        $slides[$key] = get_post_meta( $id, $key, true);

while( isset( $slides["slide{$i}"] ) ) {

    echo '<img src="' . wp_get_attachment_url( $slides["slide{$i}"] ). '" />';

Are you open to saving slides as separate posts? I've done this in the past.

Consider assigning posts to a page's slideshow with custom fields. Create a post for each picture in which you create a custom field key of "slide_show_for", and write in the parent page ID.

slide_show_for => 29

Wherever you call your slideshow, use this:


If you need strict control of the slide order, and you don't create the slide posts in the order that you wish to display them, I recommend postMash plugin to order posts with a drag and drop interface. This plugin doesn't come with the ability to choose which category of posts you wish to order. It's options panel will default to showing all posts. I've hacked the plugin code to hardcode whatever category I wish to edit. that may be beyond your comfort zone, and maybe there's another way of controlling the order, like manually editing each post's menu_order from phpmyadmin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.