Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>  
 <div><img src="foo.jpg">  
 </div> 
 <div><img src="test.jpg">   
 </div>
</div>  

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

<img src="<?php echo wp_get_attachment_url($slide1); ?>">
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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}"] ). '" />';
    $i++;
}
share|improve this answer
add comment

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:

query_posts('meta_key=slide_show_for&meta_value='.$wp->query_vars['p']);

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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